How do I change the height of UITextField height based on device using AutoLayout and IB

1.7k views Asked by At

Experts, I am struggling to dynamically scale the height of UITextField based on the screen size using AutoLayout.

For example I would like to see TextFixed height as 25 when viewed on 3.5 screen size and 50 when viewed on 4.7 screen size.

Is this even possible using IB? If not what would be the correct programmatic way to do so?

3

There are 3 answers

0
Alan Feng On

Please refer to the image below for what you need to setup in the IB. code example

autolayout constrain

control alignment

0
rdelmar On

I'm not sure why you want to do this, but it can be done in IB. Select the text field, hold down "command" and choose the main view. Choose equal heights from the pin icon at the bottom of the screen. Edit that constraint to give it a multiplier of 0.28, and a constant of -111 (reverse the order of the items if the text field is not the first item). This will give you a height of 25 on a 480 point tall screen and a height of 50 on a 568 point tall screen. These numbers come from solving two equations that define constraints,

subview.height = superview.height * multiplier + constant

50 = 568 * m + c and 25 = 480 * m + c solve these two to get the values.

0
Saikiran Komirishetty On

This can be achieved by setting promotional height constraint between text field height and it's super view height. Let's say if the superview height changes from 100 to 200 then the subview height also changes proportionally based on height factor.enter image description here