UIKit init patterns – The.Swift.Dev.


UIViewController init

Really UIViewController intialization is fairly easy. You solely should override a number of strategies if you wish to be in full management. It relies on the circumstances which init will probably be referred to as, if you’re utilizing a storyboard, init(coder) is the one that you’re on the lookout for. If you’re making an attempt to provoke your controller from an exterior nib file, init(nib,bundle) goes to be referred to as. You even have a 3rd choice, you possibly can initialize a controller programmatically from code. Lengthy story brief, with a view to make a sane init course of, it’s important to take care of all these things.

Let me introduce two patterns for UIViewControllers, the primary one is only a frequent init perform that will get referred to as in each case that might initialize a controller.

import UIKit

class ViewController: UIViewController {

    override init(
        nibName nibNameOrNil: String?, 
        bundle nibBundleOrNil: Bundle?
    ) {
        tremendous.init(
            nibName: nibNameOrNil, 
            bundle: nibBundleOrNil
        )

        self.initialize()
    }

    required init?(
        coder aDecoder: NSCoder
    ) {
        tremendous.init(coder: aDecoder)

        self.initialize()
    }

    init() {
        tremendous.init(nibName: nil, bundle: nil)

        self.initialize()
    }

    func initialize() {
        
    }
}

You can even conceal the init(nib:bundle) and init(coder) strategies from the longer term subclasses. You do not have to override init(nib:bundle) and you’ll mark the init(coder) as a comfort initializer. It looks as if somewhat bit hacky resolution and I do not prefer it an excessive amount of, however it does the job.

import UIKit

class ViewController: UIViewController {

    init() {
        tremendous.init(nibName: nil, bundle: nil)

        self.initialize()
    }

    required comfort init?(coder aDecoder: NSCoder) {
        self.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {
        
    }
}

class MyFutureViewController: ViewController {

    override init() {
        tremendous.init()
    }
}
let vc = MyFutureViewController()

UIView init

I normally create a typical initializer for UIViews to make the init course of extra nice. I additionally set the translate autoresizing masks property to false in that initializer technique, as a result of it is 2017 and noone makes use of springs & struts anymore, proper?

import UIKit

class View: UIView {

    init() {
        tremendous.init(body: .zero)

        self.initialize()
    }

    override init(body: CGRect) {
        tremendous.init(body: body)

        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {
        self.translatesAutoresizingMaskIntoConstraints = false
    }
}

It is also good to have some autolayout helpers, and if you wish to initialize a view from a nib file, it is actually good to have some comfort technique round.

import UIKit

extension UIView {

    public comfort init(autolayout: Bool) {
        self.init(body: .zero)

        self.translatesAutoresizingMaskIntoConstraints = !autolayout
    }

    public static func create(autolayout: Bool = true) -> Self {
        let _self = self.init()
        let view  = _self as UIView
        view.translatesAutoresizingMaskIntoConstraints = !autolayout
        return _self
    }

    public static func createFromNib(
        proprietor: Any? = nil, 
        choices: [AnyHashable: Any]? = nil
    ) -> UIView {
        return Bundle.primary.loadNibNamed(
            String(describing: self), 
            proprietor: proprietor, 
            choices: choices
        )?.final as! UIView
    }
}
let view = UIView(autolayout: true)

Utilizing these snippets, it is very easy to take care of a sane init course of for all of the UIKit courses, as a result of most of them ared derived from these two “main” courses.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox