๐ Configure constraints for each device in Interface Builder with IBLayoutConstraint.
IBLayoutConstraint is written in Swift 5.0 and is available on iOS/Mac Catalyst 11.0 or higher.
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsTo integrate IBLayoutConstraint into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'
pod 'IBLayoutConstraint'Then, run the following command:
$ pod installThe main thought of this framework is convinient way to configure constraint for each device in Interface Builder.
Sometimes we need to change constant/multiplier value for NSLayoutConstraint only for specific device, for example, for iPhone 5s (4").
If this UIView with layout has special constraint for one device, we should do:
- inheritance from
UIView - add
outletto this view@IBOutlet weak private var someConstraint: NSLayoutConstraint! - add some code to change constraint's value for specific device
if UIDevice.current.isIPhone5() {
someConstraint.constant = 290
}IBLayoutConstraint will takes responsibility for this cases.
(!) Notice: This approach isn't quite obvious, and you should point out it in documentation of your project.
-
Find
constraintfor configuration in Interface Builder View in Xcode. -
Go to
Indentity Inspectorfor thisconstraintin right-side panel. -
Change Class
NSLayoutConstrainttoIBLayoutConstraintIf you want to specify
multipliervalue orconstraintandmultipliertoghether, you should useIBMultiplierLayoutConstraint. -
Go to
Attributes Inspectorfor thisconstraintin right-side panel. You'll see:
-
Find the screen size in inches that matches your need. You'll see two textfields:
Width- value for landscape modeHeight- value for portrait modeFill the text fields. The necessary value will be applied after initialization and will change accordingly after device rotation.
Note: You don't need specify values for all sizes,but you need specify values for 'width' and 'height' in one line - it is required (even if you don't support rotation). If values don't specified for some size - IBLayoutConstraint will work as NSLayoutConstraint.
Issues and pull requests are welcome!
Maksim Kurpa - @maksim_kurpa
This code is distributed under the terms and conditions of the MIT license.

