2

I'm writing nib-less views in which I use autolayout for all my layout logic. I find myself having to turn off autoresizing with every view I instantiate. My code is littered with a lot of these:

view.translatesAutoresizingMaskIntoConstraints

Ideally I'd like to just

extension UIView/NSView {
    override var translatesAutoresizingMaskIntoConstraints: Bool = false
}

and get it over with once and for all, but extensions can't override stored properties.

Is there some other simple way to switch off autoresizing for good?

pseudosudo
  • 6,270
  • 9
  • 40
  • 53

1 Answers1

0

Well just a suggestion since its annoying to always set that to false, just setup a function with all the shared setups for the UIView and call it every time, its saves time and its kinda less annoying than trying and setting the values each time,

extension UIView {
func notTranslated() {
self.translatesAutoresizingMaskIntoConstraints = false
//Add any  additional code. 
    }
}
//Usage
let view = UIView()
view.notTranslated()

You can't override this constraints properties because the UIView maybe declared in the IB

translatesAutoresizingMaskIntoConstraints according to apple.

By default, the property is set to true for any view you programmatically create. If you add views in Interface Builder, the system automatically sets this property to false.

imagine if you could override that from an extension that would lead to some conflicts if there was other UIView's that's have the opposite value True || false, so in my opinion:

Apple did this to prevent any conflicts with the views constrains, therefore if you don't like to write it every time just wrap it up in a function.

Please if anyone have additional information, don't hesitate to contribute.

UPDATE: I found this cool answer that could also work, check out the code below.

class MyNibless: UIView {
    //-----------------------------------------------------------------------------------------------------
    //Constructors, Initializers, and UIView lifecycle
    //-----------------------------------------------------------------------------------------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        didLoad()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        didLoad()
    }

    convenience init() {
        self.init(frame: CGRect.zero)
    }

    func didLoad() {
        //Place your initialization code here

        //I actually create & place constraints in here, instead of in
        //updateConstraints
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        //Custom manually positioning layout goes here (auto-layout pass has already run first pass)
    }

    override func updateConstraints() {
        super.updateConstraints()

        //Disable this if you are adding constraints manually
        //or you're going to have a 'bad time'
        //self.translatesAutoresizingMaskIntoConstraints = false
        translatesAutoresizingMaskIntoConstraints = false
        //Add custom constraint code here
    }
}
var nibless: UIView = MyNibless()
//Usage
nibless.updateConstraints()
print(nibless.translatesAutoresizingMaskIntoConstraints) //false

So simply just create MyNibless instance as UIView and this also open big door to customizations too

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50