if you support only portrait mode then.
First choose a base layout for design.As you have chosen iphone 7.
I choose iphone 7 plus.
Create some constant.
Why?
To create an aspect ratio to multiply with your every constraint's constant value you drew in iphone 7 layout.so that it will be calculate properly according to the device.
Like this.
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
You can use mine too. if your base layout is iphone 7Plus.
How to use this?
Suppose you draw a top constraint about 10 for label in iphone 7 plus.
Then make a function and call it in viewDidLoad like this to support for all devices in portrait mode.
func updateConstraint() {
topConstantOfYourLabel.constant = 10*ASPECT_RATIO_RESPECT_OF_7P
}
if you support landscape mode
- if you want to change the layout for landscape then you need to design for landscape.As most of your outlets just move here and there you only need to change top bottom left right constraint. So in that case , uninstall previous constraint and install another constraint and make it work only that size class.
There are lots of online tutorial about adaptive layout like this.
if you don't want to change the layout but properly calculate according to aspect ratio, then there is a function which triggers during orientation.
override func viewWillTransitionToSize(size: CGSize,
withTransitionCoordinator coordinator:
UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
imageView.image = UIImage(named: const2)
} else {
print("Portrait")
imageView.image = UIImage(named: const)
}
}
Apply logic in that function to support for all devices:)