简体   繁体   English

为什么我不能以编程方式更改UITextField的宽度?

[英]Why I cant programmatically change the width of UITextField?

I am trying to programmatically change the width of a UITextField using the following code: 我正在尝试使用以下代码以编程方式更改UITextField的宽度:

myText.frame = CGRectMake(myText.frame.origin.x, myText.frame.origin.y, 280 ,myText.frame.size.height);

The code above is executed on a "touch up inside" button action. 上面的代码是通过“在内部进行修饰”按钮操作执行的。

myText has its border style property set to UITextBorderStyleNone myText的border style属性设置为UITextBorderStyleNone

Any ideas are welcome! 任何想法都欢迎!

如果您尝试以编程方式设置框架,只需禁用.xib的自动布局

My first thought is that either you never linked the IBAction to the UIButton, or there is no IBOutlet pointing to the UITextField. 我的第一个想法是,要么您从未将IBAction链接到UIButton,要么没有IBOutlet指向UITextField。

You can verify that the IBAction is getting called on button press simply by adding an NSLog statement to it, or you can check that both are linked from the "Connections Inspector" in Interface Builder. 您可以通过在按钮上添加一个NSLog语句来验证IBAction是否在按钮按下时被调用,或者可以从Interface Builder中的“ Connections Inspector”中检查两者是否都已链接。

Side note Since your text field is set to have no border, it is possible that you just can't tell it is growing. 旁注由于您的文本字段设置为无边框,因此您可能无法判断它在增长。 To verify, I would trying setting the text field's background color to something obvious. 为了验证,我将尝试将文本字段的背景色设置为明显的颜色。

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

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