A convenient way of enabling Auto Layout for UIView in Swift 5

Sometimes we want to enable Auto Layout when you are creating a UIView in code. We can make that process a lot easier with a simple extension.

This extension will initialize a UIView that sets translatesAutoresizingMaskIntoConstraints to the opposite of useAutolayout from the initializer and thus enables the view for Auto Layout if useAutolayout is true:

extension UIView {
    convenience init(useAutolayout: Bool) {
        self.init()
        translatesAutoresizingMaskIntoConstraints = !useAutolayout
    }
}

Now we can use it like this in our code:

let view = UIView(useAutolayout: true)

9 comments on “A convenient way of enabling Auto Layout for UIView in Swift 5”

  1. Pingback: hello world

  2. Pingback: cialis cost per pill

  3. Pingback: dapoxetine buy uk

  4. Pingback: what is finasteride

  5. Pingback: doxycycline hyclate 100 mg capsule

  6. Pingback: sertraline hcl 50 mg

  7. Pingback: lasix diuretic medication

  8. Pingback: vibramycin 100mg

  9. Pingback: vidalista 40 tadalafil

Comments are closed.