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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...