133

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?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
Arun
  • 3,478
  • 8
  • 33
  • 46

19 Answers19

217

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:

enter image description here

Brian
  • 30,156
  • 15
  • 86
  • 87
151

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.

textField.borderStyle = UITextBorderStyleRoundedRect;
nathanbroyles
  • 1,511
  • 2
  • 9
  • 3
90
CGRect frameRect = textField.frame;
frameRect.size.height = 100; // <-- Specify the height you want here.
textField.frame = frameRect;
Benjohn
  • 13,228
  • 9
  • 65
  • 127
Manjunath
  • 4,545
  • 2
  • 26
  • 31
  • 4
    it's much easier to open .xib as source find ur IBUITextField and change property – HotJard Mar 25 '13 at 06:14
  • I tried that but it didn't do anything for me. Where do you put those lines? I put it in the initWithNibName method. – janex May 07 '13 at 23:15
  • it should be in viewDidLoad or ViewWillAppear / viewDidApper – Manjunath May 13 '13 at 14:55
  • 2
    The default height of a UITextField is 31, for anyone adding one programatically and wanting it a default height – sam_smith Jun 24 '14 at 05:11
  • 3
    @HotJard I would recommend against hacking in the storyboard source, it tends to reset some of it's values. For example, when you set the textfield height like that, and you add a button to the view and edit the button's text size the textfields in the view will jump back to their default height. – Kevin Sep 10 '15 at 07:13
  • Using the above code worked well for me, but auto layout must be turned off for it to work. – Carter S. Jan 31 '17 at 02:25
85

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

enter image description here

Step 2: Select Height Constraint

enter image description here

Step 3: Change Height Constraint's constant value

enter image description here

user1046037
  • 16,755
  • 12
  • 92
  • 138
  • It worked but I got this new warning, any ideas: Position is ambiguous for "Round Style Text Field" – Esqarrouth Jan 22 '14 at 17:33
  • If it says Position is ambiguous, then it means that you You might not have a constraint to position the UITextField object. Can you try adding a constraint to define the x and y. Example you could add leading space constraint (for x) and you could add vertical space constraint from the top. – user1046037 Jan 23 '14 at 12:12
  • http://stackoverflow.com/questions/21295136/storyboard-position-is-ambiguous-for-round-style-text-field-warning when I do that I added a print screen – Esqarrouth Jan 26 '14 at 20:52
  • A tip for finding the correct height constraint for the font you are using: Set `borderStyle` to anything _except_ rounded. Use `Editor -> Size to Fit Content` to have layout do its thing. Use the property inspector on your `UITextField` to check its height. Use this to set up your height constraint, and now you can change the `borderStyle` back to the rounded one. – Benjohn Aug 06 '15 at 16:37
  • Can I do it programatically? – LKM Aug 08 '15 at 16:18
  • Sure. Just add an explicit height constraint – user1046037 Aug 08 '15 at 16:27
31

1.) Change the border Style in the InterfaceBuilder.

enter image description here

2.) After that you're able to change the size.

enter image description here

3.) Create an IBOutlet to your TextField and enter the following code to your viewDidLoad() to change the BorderStyle back.

textField.borderStyle = UITextBorderStyleRoundedRect;

Swift 3:

textField.borderStyle = UITextBorderStyle.roundedRect
Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
David Seek
  • 16,783
  • 19
  • 105
  • 136
29
  1. Choose the border style as not rounded

enter image description here

  1. Set your height

enter image description here

in your viewWillAppear set the corners as round

yourUITextField.borderStyle = UITextBorderStyleRoundedRect;

enter image description here

  1. Enjoy your round and tall UITextField
11

Follow these two simple steps and get increase height of your UItextField.

Step 1: right click on XIB file and open it as in "Source Code".

Step 2: Find the same UITextfield source and set the frame as you want.

You can use these steps to change frame of any apple controls.

VisioN
  • 143,310
  • 32
  • 282
  • 281
Sunil Targe
  • 7,251
  • 5
  • 49
  • 80
  • 1
    Funny but this is the best way :) – VisioN Apr 10 '13 at 13:34
  • 4
    This isn't best way when you think about other programmer trying to find where height is setted – bucherland Apr 29 '14 at 04:48
  • 1
    This sounds like a terrible idea. You should consider the XIB source as a private api. It may change out from under you in future version of Xcode. Not to mention you have hidden your customization in the last place any sane person will look for it in the future. – Michael Peterson Dec 01 '16 at 18:32
8

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.

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.

Guillaume Laurent
  • 1,734
  • 14
  • 11
  • 1
    This is what I am looking for... but how do I set the height-constraint so that it is expanding the height dynamically based on the content of the UITextField? – jbandi Dec 23 '12 at 13:01
  • func updateTextFields() { if #available(iOS 11, *) { } else { nameTextField.borderStyle = .line nameTextFieldHeight.constant = nameTextField.sizeThatFits(CGSize(width: 1000, height: 500)).height nameTextField.borderStyle = .roundedRect } } I want my roundBorder textFields to adjust their height based on the adaptive font I'm setting on them. This works out of the box on iOS 11 but doesn't work at all on iOS 9,10. I use the following code: – PhoneyDeveloper Mar 01 '18 at 17:02
6

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.

Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
4

swift3

@IBDesignable
class BigTextField: UITextField {
    override func didMoveToWindow() {
        super.didMoveToWindow()
        if window != nil {
            borderStyle = .roundedRect
        }
    }
}

Interface Builder

  • Replace UITextField with BigTextField.
  • Change the Border Style to none.
Community
  • 1
  • 1
neoneye
  • 50,398
  • 25
  • 166
  • 151
3

My pathetic contribution to this dumb problem. In IB set the style to none so you can set the height, then in IB set the class to be a subclass of UITextField that forces the style to be rounded rect.

@interface JLTForcedRoundedRectTextField : UITextField
@end

@implementation JLTForcedRoundedRectTextField
- (void)awakeFromNib
{
    self.borderStyle = UITextBorderStyleRoundedRect;
}
@end

It kept me from having to hack the XIB file or writing style code into my view controller.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
2

A UITextField's height is not adjustable in Attributes Inspector only when it has the default rounded corners border style, but adding a height constraint (plus any other constraints which are required to satisfy the autolayout system - often by simply using Add Missing Constraints) to it and adjusting the constraint will adjust the textfield's height. If you don't want constraints, the constraints can be removed (Clear Constraints) and the textfield will remain at the adjusted height.

Works like a charm.

Simon.
  • 1,886
  • 5
  • 29
  • 62
2

In Swift 3 use:

yourTextField.frame.size.height = 30
Kaptain
  • 1,358
  • 12
  • 20
1

You can use frame property of textfield to change frame Like-Textfield.frame=CGRECTMake(x axis,y axis,width,height)

1

This is quite simple.

yourtextfield.frame = CGRectMake (yourXAxis, yourYAxis, yourWidth, yourHeight);

Declare your textfield as a gloabal property & change its frame where ever you want to do it in your code.

Happy Coding!

Vignesh
  • 170
  • 10
  • 2
    This is a dangerous and outmoded answer. Using a global variable is a terrible practice considering it will outlive the view, and hardcoding a size will not scale well with flexible layouts. – Chris Conover Mar 21 '15 at 04:33
1

If you're creating a lot of UITextFields it can be quicker to subclass UITextViews and override the setFrame method with

-(void)setFrame:(CGRect)frame{
    [self setBorderStyle:UITextBorderStyleRoundedRect];
    [super setFrame:frame];
    [self setBorderStyle:UITextBorderStyleNone];
}  

This way you can just call
[customTextField setFrame:<rect>];

mylogon
  • 2,772
  • 2
  • 28
  • 42
1

I was having the same issue. tried some of the solutions here but rather than doing all this mumbo-jumbo. I found just setting height constraint is enough.

Alok C
  • 2,787
  • 3
  • 25
  • 44
1

try this

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 280, 120)];
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
1
UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [txt setText:@"Ananth"];
    [self.view addSubview:txt];

Last two arguments are width and height, You can set as you wish...

User-1070892
  • 929
  • 10
  • 16