قالب وردپرس درنا توس
Home / IOS Development / Mastering iOS auto setup is anchored programmatically from Swift

Mastering iOS auto setup is anchored programmatically from Swift



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:

  • loadView
  • viewDidLoad
  • viewWillAppear
  • viewWillLayoutSubviews
  • viewDidLayoutSubviews
  • viewDidAppear

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 loadView and viewDidLoad methods. 🤓

These are the basic rules for creating viewing hierarchies using automatic setup:

  • Never calculate frames manually by yourself! ⚠️ ️️️️
  • Initialize your views with .zero straight frame
  • Set translatesAutoresizingMaskIntoConstraints to false
  • Add your view to the view hierarchy using Add Create and activate your layout restrictions NSLayoutConstraint.activate
  • Use loadView instead of viewDidLoad to create views with limitations
  • Take care of memory management using weak properties
  • Set all other features as background colors, etc. in viewDidLoad

Another theory, here's a brief example:

  • 1
    9659021] Simply, huh? Just a few lines of code, and you have a fixed size center-aligned view with a dedicated classroom reference. If you create exactly the same interface builder, the system will "create" you loadView method for free, but you must set up a IBOutlet reference to the view. [19659022] 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 SafeAreaLayoutGuide property. You can get all the usual anchors after you call the safe area. 💪

     Become a cartridge!

    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.

  • Rules:

    • Use default UIView animation with layoutIfNeeded
    • 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. 🤐

    Rotational Support

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

    Traits Collections

    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.

    Device Detection

    You can also check the user interface idom via UIDevice

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

    Screen Size

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


    External sources


Source link