UIButton with GradientLayer obscures image and darkens gradient(带有 GradientLayer 的 UIButton 使图像模糊并使渐变变暗)
问题描述
我在这里有一个 UIButton,我希望将渐变作为图像下方的背景(具有透明背景的符号),但我面临两个不同的问题.
I have an UIButton here where I'd like to have a gradient as the background below the image (symbol with transparent background), but I'm facing two different problems.
无论我如何尝试添加,CAGradientLayer 的第一个似乎都覆盖在图像之上,完全遮蔽了图像.
First of the CAGradientLayer seems to overlay on top of the image no matter how I try to add it, obscuring the image completely.
其次,渐变本身似乎变暗了很多,就像按钮被禁用一样,但事实并非如此.
Secondly the gradient itself seems to be darkened a lot, like the button was disabled, which it isn't.
这是我的代码:
self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 35, 28, 28)];
[backButton addTarget:self
action:@selector(goBack)
forControlEvents:UIControlEventTouchUpInside];
[backButton setBackgroundColor:[UIColor clearColor]];
CAGradientLayer *buttonGradient = [CAGradientLayer layer];
buttonGradient.frame = backButton.bounds;
buttonGradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:.0
green:.166
blue:.255
alpha:1] CGColor],
(id)[[UIColor colorWithRed:.0
green:.113
blue:.255
alpha:1] CGColor],
nil];
[buttonGradient setCornerRadius:backButton.frame.size.width / 2];
[backButton.layer insertSublayer:buttonGradient
atIndex:0];
[backButton setImage:[UIImage imageNamed:@"backarrow.png"]
forState:UIControlStateNormal];
[backButton setEnabled:NO];
[topbarView addSubview:backButton];
推荐答案
所以我设法通过在添加渐变后执行 [button bringSubviewToFront:button.imageView] 来解决这个问题.似乎无论我做什么,新层都会添加到 imageView 之上,所以我需要在之后将它推到前面.
So I managed to get around this by doing a [button bringSubviewToFront:button.imageView] after adding the gradient. Seems that no matter what I do the new layer will add on top of the imageView, so I need to push that to the front afterwards.
目标-C:
[button bringSubviewToFront:button.imageView]
Swift 4.1:
button.bringSubview(toFront:button.imageView!)
这篇关于带有 GradientLayer 的 UIButton 使图像模糊并使渐变变暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 GradientLayer 的 UIButton 使图像模糊并使渐变变暗
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01