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);

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...