UIButton:添加渐变图层和标题不再显示 - 如何修复?

Kru*_*lur 8 cocoa-touch xamarin.ios

我正在使用下面的代码向UIButton添加渐变图层.工作正常,但标题不再可见.有人知道怎么修理吗?

UIButton oAddressBtn = UIButton.FromType (UIButtonType.Custom);
oAddressBtn.Frame = new RectangleF (0, 0, 150, 25);
oAddressBtn.VerticalAlignment = UIControlContentVerticalAlignment.Center;
oAddressBtn.Font = UIFont.FromName("Helvetica", 12);
oAddressBtn.SetTitleColor (UIColor.White, UIControlState.Normal);

// Create a gradient for the background.
CAGradientLayer oGradient = new CAGradientLayer ();
oGradient.Frame = oAddressBtn.Bounds;
oGradient.Colors = new CGColor[] { UIColor.FromRGB (170, 190, 235).CGColor, UIColor.FromRGB (120, 130, 215).CGColor };

// Assign gradient to the button.
oAddressBtn.Layer.MasksToBounds = true;
oAddressBtn.Layer.AddSublayer (oGradient);
oAddressBtn.Layer.CornerRadius = 10;
oAddressBtn.Layer.BorderColor = UIColor.FromRGB (120, 130, 215).CGColor;

// Set the button's title.
oAddressBtn.SetTitle (sAddress, UIControlState.Normal);
Run Code Online (Sandbox Code Playgroud)

Kru*_*lur 14

哈!问一个问题,然后自己找出来......巧合.我不得不改变顺序.分配渐变后,必须设置按钮的文本属性,而不是之前.固定代码在这里:

UIButton oAddressBtn = UIButton.FromType (UIButtonType.Custom);
oAddressBtn.Frame = new RectangleF (0, 0, 150, 25);

// Create a gradient for the background.
CAGradientLayer oGradient = new CAGradientLayer ();
oGradient.Frame = oAddressBtn.Bounds;
oGradient.Colors = new CGColor[] { UIColor.FromRGB (170, 190, 235).CGColor, UIColor.FromRGB (120, 130, 215).CGColor };

// Assign gradient to the button.
oAddressBtn.Layer.MasksToBounds = true;
oAddressBtn.Layer.AddSublayer (oGradient);
oAddressBtn.Layer.CornerRadius = 10;
oAddressBtn.Layer.BorderColor = UIColor.FromRGB (120, 130, 215).CGColor;

// Set the button's title. Alignment and font have to be set here to make it work.
oAddressBtn.VerticalAlignment = UIControlContentVerticalAlignment.Center;
oAddressBtn.Font = UIFont.FromName("Helvetica", 12);
oAddressBtn.SetTitleColor (UIColor.White, UIControlState.Normal);

oAddressBtn.SetTitle (sAddress, UIControlState.Normal);
Run Code Online (Sandbox Code Playgroud)

  • 我找到了解决问题的另一种方法.不要使用Layer.AddSublayer API,而是使用Layer.insertSublayer atIndex API并将索引设置为0.(我的应用程序是用OC编写的,我不知道Swift API ...) (3认同)

小智 5

对于添加子层的按钮不起作用。在标题标签下方插入子图层对我有用。这是参考代码:

-(void)addGradientOnButtonWithStartColor:(UIColor *)startColor mediumColor:(UIColor *)mediumColor andEndColor:(UIColor *)endColor forButton:(UIButton *)customButton andTitle:(NSString *)buttonTitle{

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = customButton.layer.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:
                        (id)startColor.CGColor,
                        (id)mediumColor.CGColor,
                         (id)endColor.CGColor,
                        nil];

gradientLayer.locations = nil;
gradientLayer.masksToBounds = YES;
gradientLayer.cornerRadius = customButton.layer.cornerRadius;
[customButton.layer insertSublayer:gradientLayer below:customButton.titleLabel.layer];


[customButton setTitle:buttonTitle forState:UIControlStateNormal];
[customButton setTitle:buttonTitle forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)

}

在 viewDidAppear 方法中调用此方法。

谢谢,拉特内什瓦尔