Skip to content

๐Ÿ“ Configure constraints for each device in Interface Builder with IBLayoutConstraint.

License

Notifications You must be signed in to change notification settings

MaksimKurpa/IBLayoutConstraint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

IBLayoutConstraint

๐Ÿ“ Configure constraints for each device in Interface Builder with IBLayoutConstraint.

Requirements

IBLayoutConstraint is written in Swift 5.0 and is available on iOS/Mac Catalyst 11.0 or higher.

Installation

Dependency Managers

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To 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 install

Introduction

The 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:

  1. inheritance from UIView
  2. add outlet to this view @IBOutlet weak private var someConstraint: NSLayoutConstraint!
  3. 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.

How to use

(!) Notice: This approach isn't quite obvious, and you should point out it in documentation of your project.
  1. Find constraint for configuration in Interface Builder View in Xcode.

  2. Go to Indentity Inspector for this constraint in right-side panel.

  3. Change Class NSLayoutConstraint to IBLayoutConstraint

    If you want to specify multiplier value or constraintand multiplier toghether, you should use IBMultiplierLayoutConstraint.

  4. Go to Attributes Inspector for this constraint in right-side panel. You'll see:

IBLayoutConstraint

IBLayoutConstraint

IBMultiplierLayoutConstraint

IBLayoutConstraint
  1. Find the screen size in inches that matches your need. You'll see two textfields:

    Width - value for landscape mode

    Height - value for portrait mode

    Fill 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.

Contributing

Issues and pull requests are welcome!

Author

Maksim Kurpa - @maksim_kurpa

License

This code is distributed under the terms and conditions of the MIT license.