Reputation: 2308
I have a tableView with static cells. The cell contains an ImageView which fills it completely. And I have another smaller ImageViews atop. I position this ImageViews with constraints. I have a question about resizing the constraints. How can I set different constraint constants for different devices sizes without programming if/else loops. Is there a way to set it in the storyboard? I have a leading constraint to parent layout for example with an constant value of 10. This is okay for the iPhone 5 screen, but on an iPhone 6/ 6 plus screen it should be higher than 10.
iPhone5 Autolayout
iPhone6 Autolayout
Upvotes: 33
Views: 40439
Reputation: 502
You can use BayKit for this job.
What is BayKit:
Calculates the global offset for all screen sizes by depending on given screen size and given offset.
How to use BayKit: Usage is very easy, just import BayKit and set constant value with BayKit like this:
import BayKit
class MyViewController: UIViewController {
lazy var box = UIView()
let magicOffset = BayKit()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(box)
box.translatesAutoresizingMaskIntoConstraints = false
box.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: magicOffset.offseter(scaleFactor: 1.0,
offset: 24, direction: .horizontal, currentDeviceBound: BayKit.DeviceList.iPhone5.screenWidth)).isActive = true
}
}
As you can see this line calculating offset for all screen sizes:
magicOffset.offseter(scaleFactor: 1.0, offset: 24, direction: .horizontal, currentDeviceBound: BayKit.DeviceList.iPhone5.screenWidth)
Offseter Features:
Example Result:
Upvotes: 2
Reputation: 5957
Very easy. You can change constants values for difference size classes in Storyboard. I am giving u a few steps after which you will be able to grasp it.
First we create constants as you can see on this view
Next we select the constant we want to change the value in other size classes.
Now comes the tricky part. In the attribute inspector after selecting the constant, you can see the value of the constant. Right beside you can see the PLUS (+) sign, left of the "constant".
Click on it and select your size class that you want. Here i have selected Regular Height Regular Width i.e for iPad sizes.
Next we give it a new value. So the constant, which normally is of value 61, will function as 10 when rendered in a size class of iPad size classes.
Here is the output --
iPhone 4:
iPad Air:
As you can see, the same constants has different value in runtime corresponding to different size classes.
Hope my explanation helped you.
Upvotes: 59
Reputation: 3526
Once my junior developer asked me the same question that how can I differentiate between iPhoneSE and iPhone6 for some constraint at that time there was only one solution that was writing some thing like
if device == iPhoneSE {
constant = 44
} else if device == iPhone6 {
constant = 52
}
To overcome this issue I created a library Layout Helper so now you can update constraint for each device without writing a single line of code.
Upvotes: 22
Reputation: 2308
Finally, I`ve found the solution which works in my case.
As the result, I have really adaptive Autolayout which works almost great on iPhones 5-6-6+
Upvotes: 4