ios 如何设置 UITextField 高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8641557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set UITextField height?
提问by Arun
I am using a UITextField
. I want to increase its height but I have not found any property to do this. How can I achieve this?
我正在使用一个UITextField
. 我想增加它的高度,但我还没有找到任何属性来做到这一点。我怎样才能做到这一点?
采纳答案by Manjunath
CGRect frameRect = textField.frame;
frameRect.size.height = 100; // <-- Specify the height you want here.
textField.frame = frameRect;
回答by Brian
You can not change the height of the rounded rect border style. To set the height, just choose any border style other than rounded border in Xcode:
您无法更改圆角矩形边框样式的高度。要设置高度,只需在 Xcode 中选择圆形边框以外的任何边框样式:
回答by nathanbroyles
I finally found the fix for this!
我终于找到了解决方法!
As we have found, IB doesn't allow us to change the height of the rounded corner border style. So change it to any of the other styles and set the desired height. In the code change the border style back.
正如我们所发现的,IB 不允许我们更改圆角边框样式的高度。因此,将其更改为任何其他样式并设置所需的高度。在代码中将边框样式改回来。
textField.borderStyle = UITextBorderStyleRoundedRect;
回答by user1046037
If you are using Auto Layout then you can do it on the Story board.
如果您使用自动布局,那么您可以在故事板上进行。
Add a height constraint to the text field, then change the height constraint constant to any desired value. Steps are shown below:
向文本字段添加高度约束,然后将高度约束常量更改为任何所需值。步骤如下所示:
Step 1: Create a height constraint for the text field
步骤 1:为文本字段创建高度约束
Step 2: Select Height Constraint
步骤 2:选择高度约束
Step 3: Change Height Constraint's constant value
第 3 步:更改高度约束的常量值
回答by Deprecated Darren
回答by David Seek
1.) Change the border Style in the InterfaceBuilder.
1.) 在 InterfaceBuilder 中更改边框样式。
2.) After that you're able to change the size.
2.) 之后,您可以更改大小。
3.) Create an IBOutlet to your TextField and enter the following code to your viewDidLoad()
to change the BorderStyle back.
3.) 为您的 TextField 创建一个 IBOutlet 并输入以下代码以viewDidLoad()
将 BorderStyle 改回。
textField.borderStyle = UITextBorderStyleRoundedRect;
Swift 3:
斯威夫特 3:
textField.borderStyle = UITextBorderStyle.roundedRect
回答by Sunil Targe
Follow these two simple steps and get increase height of your UItextField
.
按照这两个简单的步骤来增加你的UItextField
.
Step 1:right click on XIB file and open it as in "Source Code".
第 1 步:右键单击 XIB 文件并像“源代码”一样打开它。
Step 2:Find the same UITextfield
source and set the frame as you want.
第 2 步:找到相同的UITextfield
来源并根据需要设置框架。
You can use these steps to change frame of any apple controls.
您可以使用这些步骤来更改任何苹果控件的框架。
回答by Guillaume Laurent
An update for iOS 6 : using auto-layout, even though you still can't set the UITextField's height from the Size Inspector in the Interface Builder (as of Xcode 4.5 DP4 at least), it is now possible to set a Height constraint on it, which you can edit from the Interface Builder.
iOS 6 的更新:使用自动布局,即使您仍然无法从 Interface Builder 中的 Size Inspector 设置 UITextField 的高度(至少从 Xcode 4.5 DP4 开始),现在可以在上设置高度约束它,您可以从 Interface Builder 进行编辑。
Also, if you're setting the frame's height by code, auto-layout may reset it depending on the other constraints your view may have.
此外,如果您通过代码设置框架的高度,自动布局可能会根据您的视图可能具有的其他约束来重置它。
回答by Mark McCorkle
I know this an old question but I just wanted to add if you would like to easily change the height of a UITextField from inside IB then simply change that UITextfield's border type to anything other than the default rounded corner type. Then you can stretch or change height attributes easily from inside the editor.
我知道这是一个老问题,但我只是想补充一下,如果您想从 IB 内部轻松更改 UITextField 的高度,然后只需将该 UITextfield 的边框类型更改为默认圆角类型以外的任何类型。然后,您可以从编辑器内部轻松地拉伸或更改高度属性。
回答by neoneye
swift3
迅捷3
@IBDesignable
class BigTextField: UITextField {
override func didMoveToWindow() {
super.didMoveToWindow()
if window != nil {
borderStyle = .roundedRect
}
}
}
Interface Builder
界面生成器
- Replace
UITextField
withBigTextField
. - Change the
Border Style
tonone
.
- 替换
UITextField
为BigTextField
。 - 将 更改
Border Style
为none
。