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

标签 c# ios xamarin uibutton xamarin.ios

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

这是我在 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);

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

最佳答案

你必须添加它到你的UIView (UIViewController)

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

Add

This is an alias for UIView.AddSubview, but uses the Add pattern as it allows C# 3.0 constructs to add subviews after creating the object.

关于c# - 以编程方式创建按钮单点触控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38413544/

相关文章:

android - OnLocationChanged 在不同设备上以不同时间间隔触发

c# - 在同一个 UIViewController 中的第一个 UITableView 的 RowClick 上绑定(bind)第二个 UITableView - ios Xamarin

c# - 在数据库中更新抛出 SqlException

c# - 如何将纯色设置为整个菜单的背景?

c# - 多维数组的一个索引的大小

iOS 11 模拟器在 safari 中无法打开 facebook 登录 - Cordova Facebook4

ios - 在 iOS 上强制回收 JavaScriptCore 虚拟机的垃圾

c# - FCM - 重新调试应用程序后发送消息时出现 Android Xamarin NotRegistered 错误

c# - 如何从列表/数组中删除对象属性?

iphone - 在cocos2d中启动和停止粒子系统