Looking for best practice using layout anchors? Let's learn how to use the iOS autolayout system properly using Swift.
Programmed Views and Limitations
First and foremost, I would like to recreate UIViewController life cycle methods, you may be familiar with some of them. They are called in the following order:
In the pre-auto layout era, you had to do the setup calculations inside
viewDidLayoutSubviews method, but since this is a pro auto layout training, we will only focus on
viewDidLoad methods. 🤓
These are the basic rules for creating viewing hierarchies using automatic setup:
- Never calculate frames manually by yourself! ⚠️ ️️️️
- Initialize your views with
- Add your view to the view hierarchy using
Add Create and activate your layout restrictions
viewDidLoadto create views with limitations
- Take care of memory management using
- Set all other features as background colors, etc. in
Another theory, here's a brief example:
loadViewmethod for free, but you must set up a
IBOutletreference to the view.  The eternal dilemma: code vs. interface constructor.
It does not matter, please choose your way. Sometimes I love playing with IB, but in most cases I prefer the programmatic way to do things. 😛
Common UIKit auto layout limitation use cases
So I promised that I'll show you how to make limitations programmatically, right? Let's do it now. First of all, I use nothing but layout anchors. You can waste your time with the visual format language, but it's definitely a dead end. Then mark my words: just use anchor or stack views, but nothing else! 😇
Here are the most common patterns I use to create great layouts. 😉
Stick with or height
The first one is the easiest: Set a viewing height or a width to a fixed point.
Specify Size Ratio
Settings A viewing ratio provides only limitation width to height or vica versa, you can only define the frequency at the multiplier.
Center Horizontal and Vertical
Center views in another are a trivial task, it's specific anchors for it.
Stretch | fill inside view with upholstery
The only difficult part here is that back and bottom limits behave slightly differently than top and leading when it comes to constants. Usually you have to work with negative values, but after some attempt you will understand the logic here. 😅
Proportional width or height
If you do not want to work with constant values, you can use the multiplier.
Using Secure Site Setup Wizards
With the latest iPhone, you need some guides to keep you safe from hacking. This is why the views have the
SafeAreaLayoutGuideproperty. You can get all the usual anchors after you call the safe area. 💪
Animation with limitations
Animation with limitations is simple, you should not believe what others can say. rules and an example that will help you understand the basic principles of animating constant values of a limitation, as well as changing different limitations.
- Use default UIView animation with
- Always deactivate restrictions first
- Stick to your disabled restrictions
- Have fun!
Limitation animation example:
It's not bad, next: Customize and support multiple screen sizes on a device.
How to create adaptive layouts for iOS?
Even Apple fights with adaptive layouts in the embedded iOS applications. If you're watching apps created with collections - like pictures - the layouts are pretty nice on all devices. are some other ones that in my opinion are horrendous experiences on a larger screen. #justusecollectionviewforeverything. 🤐
Your first step to adaptive layout supports multiple device directions. You can check my previous article on iOS billayout. There are many good things in this article about rotational support, working with teams in auto-setup countries, etc. 🌈
Other steps are to customize pull collections. UITraitCollection is for you to group all environment-specific features like size classes, display scale, user interface idom and many more. Most times, you must check vertical and horizontal size classes. There is a reference for device size classes and all possible variations that Apple has done, see the section below for external sources.
You can also check the user interface idom via
. This small Swift code example below shows how to check size classes to set different layouts for compact and common screens. class (aka. Is this freakin device an iPhone or an iPad?) For example, enter font sizes based on it. 📱
Another option to determine your environment is to check the size of the screen. You can check the original pixel count or a relative size based on points.
Usually, I try to stick to these rules. I really do not remember a scenario where I needed more than all I mentioned above, but if you have a specific case or question please do not hesitate to contact me. 😉
- Use default UIView animation with