简体   繁体   English

使用MonoTouch以编程方式将UITextField添加到UIView

[英]Programmatically add UITextField to UIView with MonoTouch

I am learning to use MonoTouch. 我正在学习使用MonoTouch。 I have a screen with two buttons. 我有一个带两个按钮的屏幕。 When a user clicks a button, a view that I have programmatically created will appear and hide the other. 当用户单击按钮时,将以编程方式创建的视图将显示并隐藏另一个视图。 I have this successfully working. 我有这个成功的工作。 In one of my views, I want to show a text box. 在我的一个观点中,我想显示一个文本框。 While I believe that I have created / add the the text box correctly to my view, it is not appearing. 虽然我相信我已经在我的视图中正确创建/添加了文本框,但它并没有出现。 My initialization code is shown here: 我的初始化代码如下所示:

RectangleF rectangle1 = new RectangleF(0, 100, 200, 200);
this.view1 = new UIView(rectangle1);
this.View.Add(view1);

RectangleF rectangle2 = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, 300);
this.view2 = new UIView(rectangle2);
this.view2.Hidden = true;
this.view2.BackgroundColor = UIColor.Green;

UITextField textField = new UITextField();
textField.Bounds = new RectangleF(20, 13, 200, 31);
view2.AddSubView(textField);

this.View.Add(view2);

What am I doing wrong? 我究竟做错了什么? Why isn't the textField appearing? 为什么textField不出现?

Thank you! 谢谢!

Replace 更换

textField.Bounds = new RectangleF(20, 13, 200, 31); textField.Bounds = new RectangleF(20,13,200,31);

with

textField.Frame = new RectangleF(20, 13, 200, 31); textField.Frame = new RectangleF(20,13,200,31);

Your textField is a subview of View2 . 您的textField是View2子视图 Since you are setting view2.Hidden = true , it is also hiding ALL of the subviews of View2, which includes the text field. 由于您正在设置view2.Hidden = true ,因此它还隐藏了View2的所有子视图,其中包含文本字段。

Another thing you need to watch out for is what view is "on-top". 另外需要注意的是视图是“顶部”。 Try playing around with the SendSubviewToBack(UIView view) and BringSubviewToFront(UIView view) methods of UIView. 尝试使用SendSubviewToBack(UIView view)SendSubviewToBack(UIView view)BringSubviewToFront(UIView view)方法。

Have you tried removing and or changing the order of how you add the view I believe all you need is 您是否尝试删除和/或更改添加视图的顺序我相信您需要的是

this.view2.AddSubView(textField);
or     
this.View.AddSubview(view2); 

It's probably because you mark view2 as hidden. 这可能是因为您将view2标记为隐藏。 Therefore, the entire view isn't even visible. 因此,整个视图甚至都不可见。 If you remove the this.view2.Hidden = true you should see your text field. 如果你删除this.view2.Hidden = true,你应该看到你的文本字段。

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

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