Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid having to put setTranslatesAutoresizingMaskIntoConstraints(false) everywhere

Tags:

ios

I was wondering, now with Auto Layout you have to tell every UIView that you don't want to translate its (deprecated) auto-resizing masks to layout constraints like so:

let view = MyView()
view.setTranslatesAutoresizingMaskIntoConstraints(false)

In my app, I do (almost) all my views with custom constraints so I never want automatic translation of auto-resizing masks. Wouldn't it be nice if the default setting for this would be false? So only in situations where I DO want translation of auto resizing masks I set it to true?

Is there a way to make false the default for setTranslatesAutoresizingMaskIntoConstraints? Maybe with a clever extension (a.k.a. category) of some sort?

like image 655
Tom van Zummeren Avatar asked Dec 11 '25 18:12

Tom van Zummeren


1 Answers

I fully agree which is why I made this category which includes such an initialiser:

extension UIView {

    // MARK: Initializing a View Object

    /**
    * @name Initializing a View Object
    */

    /**
    *  Returns a frameless view that does not automatically use autoresizing (for use in autolayouts).
    *
    *  @return A frameless view that does not automatically use autoresizing (for use in autolayouts).
    */
    class func autoLayoutView() -> Self {
        let view = self()
        view.setTranslatesAutoresizingMaskIntoConstraints(false)
        return view
    }
}

There is a swift branch in the project as well, but this is very early stage. Pull requests welcome!

like image 79
jrturton Avatar answered Dec 13 '25 08:12

jrturton