簡體   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