Though i am a bit late and i faced the exact problem before.However.
Step 1:
Take a base screen for design. I choose iphone 7 plus.
Step 2:
Define some constants.
Why?
We need to multiply it with the autolayout constraint's constant.
Why auto-layout?
If you want to adapt your app in all of the devices, you must use
auto-layout and size classes.
I am giving you my constant:
var SCREEN_WIDTH = UIScreen.main.bounds.size.width
var SCREEN_HEIGHT = UIScreen.main.bounds.size.height
var BASE_SCREEN_HEIGHT:CGFloat = 736.0
var SCREEN_MAX_LENGTH = max(SCREEN_WIDTH, SCREEN_HEIGHT)
var ASPECT_RATIO_RESPECT_OF_7P = SCREEN_MAX_LENGTH / BASE_SCREEN_HEIGHT
Step 3:
You are done. Now how to use this. Suppose you did auto-layout to a button which has leading constant 30
in viewDidLoad
just write:
yourButtonLeadingConstraint.constant = 30 * ASPECT_RATIO_RESPECT_OF_7P
Only for this line it will maintain 30Px aspect ratio in all other devices.
Step 4:
What about Device orientation?
There is a function called
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
//write your code here such changing autolayout value
} else {
print("Portrait")
// write your code here such changing autolayout value
}
}
Utilize this method to handle orientation change.
Note:
If you want to change your Layout for landscape mode you can do it too.No
There are many tutorials on how to do it.I think you can google it and find suitable answer.