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

时间:2011-01-24 09:16:59

标签: 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);

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

我不知道如何在monotouch中完成这项工作。但下面的答案是Obj-C变体,用于您不必添加子图层的方法。

您也可以覆盖方法+layerClass以返回CAGradientLayer类,而不是插入渐变图层。按钮的图层比该类的图层要多,您可以轻松设置其颜色等。

+ (Class)layerClass {
    return [CAGradientLayer class];
}

答案 2 :(得分:2)

另一种选择是使用第三方工具调用Pixate。 http://www.pixate.com/ 此工具允许您使用CSS3并以这种方式设置iOS控件样式。这样你就可以消除所有低级控制的细微差别。

现在使用它,真正享受熟悉。一个设计可以为你构建CSS文件,你会很高兴!

答案 3 :(得分:2)

对于添加子图层的按钮,没有为它工作。在标题标签下面插入子图块对我有用。这是参考代码:

- (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];

}

在viewDidAppear方法中调用此方法。

谢谢, Ratneshwar