我正在尝试创建一个自定义的UIButton,它继承自UIButtonType.RoundedRect
。
我的附加功能可以工作,但是我的按钮的初始圆角边框状态存在问题。我的扩展按钮的边框直到被点击后才会被绘制出来。
更新(2013年1月24日):根据Richard Marskell的要求添加了红色背景测试结果,该结果表明只有按钮的标签被绘制:BackgroundColor = UIColor.Red;
下面是我创建自定义按钮的源代码。
public class TestView : UIView
{
public TestView(IntPtr p) : base(p) { }
public TestView(RectangleF bounds)
{
Frame = bounds;
BackgroundColor = UIColor.White;
UIButton button1 = new UIButton(UIButtonType.RoundedRect);
button1.Frame = new RectangleF(20,20,100,50);
button1.SetTitle("Button 1", UIControlState.Normal);
AddSubview(button1); // Drawn Correctly
MyButton button2 = new MyButton();
button2.Frame = new RectangleF(20,90,100,50);
button2.SetTitle("Button 2", UIControlState.Normal);
AddSubview(button2); // Only drawn correctly after Tap
// EDIT: Added to test Miguel's theory
UIButton button3 = UIButton.FromType(UIButtonType.RoundedRect);
button3.Frame = new RectangleF(20,160,100,50);
button3.SetTitle("Button 3", UIControlState.Normal);
AddSubview(button3); // Drawn Correctly
}
}
public class MyButton : UIButton
{
public MyButton() : base(UIButtonType.RoundedRect) { }
}
- 我不确定如何在视图加载时正确绘制边框。
- 我不需要自定义按钮类型
UIButtonType.Custom
,因为我不想自己设置按钮样式。 - 当我进行调试时,MyButton的类型已正确设置为
UIButtonType.RoundedRect
。 - MyButton(button2)的基本属性与UIButton实例(button1)的属性匹配。
我该如何解决这个问题?
更新(2013年1月31日):Herman Schoenfeld为此漏洞提供了适当的解决方案。
UIButtonType
参数的构造函数不同。 - ardilaMyButton button2 = new MyButton();
替换为UIButton button2 = new UIButton(UIButtonType.RoundedRect);
或者反过来,看看按钮是否仍然正确显示。 - Stephen Hewlett