How to make any subview flexible with respect to the frame of the type of iPhone i.e., iPhone 5, 5S, 6, 6+?

90 Views Asked by At

I have to add a text filed and it is 210X30 for iPhone 5. I want it to stretched according to iPhone 6 and iPhone6+. I don't want to fix the width or height of the text field and also don't want to fix the horizontal spacing from the edges of the iPhone. I want it to be flexible and maintain the ratio of the space which it has in case of iPhone 5. Kindly let me know what kind of Constraints I need to put in order to achieve that. Please let me know if my question is Unclear I will explain in detail as I am sure this scenario will be faced by each and every iOS Developer shortly.

1

There are 1 best solutions below

1
On BEST ANSWER

It seems like you want to keep the aspect ratio: i.e. 210x30 on an iPhone 5 screen with a width of 320, but approximately 246x35 on an iPhone 6 with a width of 375.

In Interface Builder, you can add an Aspect Ratio constraint. In code, you can add

    [NSLayoutConstraint constraintWithItem:self.label 
       attribute:NSLayoutAttributeHeight 
       relatedBy:NSLayoutRelationEqual 
       toItem:self.label 
       attribute:NSLayoutAttributeWidth 
       multiplier:210.0/30.0 
       constant:0];