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

前端之家收集整理的这篇文章主要介绍了cocoa-touch – UIButton:添加渐变图层和标题不再显示 – 如何修复?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用下面的代码向UIButton添加渐变图层.工作正常,但标题不再可见.有人知道怎么修理吗?
UIButton oAddressBtn = UIButton.FromType (UIButtonType.Custom);
oAddressBtn.Frame = new RectangleF (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,215).CGColor;

// Set the button's title.
oAddressBtn.SetTitle (sAddress,UIControlState.Normal);

解决方法

哈!问一个问题,然后自己找出来……巧合.
我不得不改变顺序.分配渐变后,必须设置按钮的文本属性,而不是之前.固定代码在这里:
UIButton oAddressBtn = UIButton.FromType (UIButtonType.Custom);
oAddressBtn.Frame = new RectangleF (0,25);

// Create a gradient for the background.
CAGradientLayer oGradient = new CAGradientLayer ();
oGradient.Frame = oAddressBtn.Bounds;
oGradient.Colors = new CGColor[] { UIColor.FromRGB (170,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",UIControlState.Normal);

oAddressBtn.SetTitle (sAddress,UIControlState.Normal);
原文链接:https://www.f2er.com/iOS/332751.html

猜你在找的iOS相关文章