繁体   English   中英

以编程方式创建按钮单点触控

[英]Create button programmatically monotouch

大家好,我试图通过代码动态创建一个按钮,但是当我运行调试器时,该按钮未在模拟器中显示。

这是我在viewdidload方法中编写的代码

var button = UIButton.FromType(UIButtonType.RoundedRect);
            button.Frame = new System.Drawing.RectangleF(100f, 100f, 100f, 100f);
         button.SetTitle("click me", UIControlState.Normal);

我也尝试过其他代码,例如:

UIButton button = new UIButton(UIButtonType.InfoDark); //BTN
button.Frame = new CoreGraphics.CGRect(59, 59, 59, 59);

我认为问题很愚蠢,但我找不到解决方案。 谢谢

您必须将其Add到您的UIViewUIViewController

UIButton button = new UIButton(UIButtonType.InfoDark); //BTN
button.Frame = new CoreGraphics.CGRect(59, 59, 59, 59);
Add(button);

这是UIView.AddSubview的别名,但是使用“添加”模式,因为它允许C#3.0构造在创建对象后添加子视图。

您分配了按钮,但未将其添加到视图中。 尝试这个

self.view.addSubview(button)

在Xamarin中,

this.AddSubview(button);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM