Swift — Auto Layout & Binding Methods Programmatically (2/4)

Let’s remember what we did in the last part of the series;
We showed how to add UI components to the view hierarchy then set its properties programmatically. Great! 🎉
In that part, I’ll describing you, how you’ll set your project via Omar Albeik’s way. Remember:


✅ Project structure looks under control.
Auto Layout Programmatically

As you know, iOS 11 or above needs to use Safe Area Layout Guides to serve layout properly. However, you must use the associated anchors.
I prepared the extension that returns the safeAreaLayoutGuide properties if the device compatible to iOS 11 or above. At the same time, that properties returns the normal anchors if your device is not compatible with iOS 11.
- It prevents you from repeating continuously, so you can use auto layout quickly.
Let’s check it; 🔥
Usage example:

layoutViews is an override method of our new way. It’s best place to set our constraints for getting fit layout. 👩🏼🍳
When you needs to create a new UIView for your controllers, you have to inherit View class.
Binding Methods & Communicate with VC’s
I think you are very curious about this. Because, there are lots of abstraction here, so you have to decide where it’ll be place your binding methods.
Let’s check my implementation:
We are using delegation to communicate with controllers.
Let’s check controller too;
Sample project on GitHub: https://github.com/strawb3rryx7/ProgrammaticUI-Sample
There is no any model interaction right now, It’s made for serving how to creating UI stuffs programmatically.
Notes:
- Use view controllers to do network calls.
- Do not move DataSources & Delegates of UI components to controllers.
If you have any questions or suggestions, don’t hesisate to reach me:
Thanks for reading.
Spread with ❤️🔥
Let’s join to WHOKNOWS!

We are a publication that creates posts about iOS development, If you liked it and wants to join us, let’s fill the form! tinyurl.com/jointowhoknows

