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

Batuhan Saka
Sep 5, 2018 · 3 min read

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:


Image 1

✅ Project structure looks under control.

Auto Layout Programmatically

safe area ile ilgili görsel sonucu

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:

Refers to Image 1 — Login Screen

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

WHOKNOWS

We produces contents about iOS / Swift development.

Batuhan Saka

Written by

iOS Developer |  WWDC18 Scholar

WHOKNOWS

WHOKNOWS

We produces contents about iOS / Swift development.