简体   繁体   中英

Constraints aren't being added through updateViewConstraints

I am making a custom keyboard and I have a function named setButtonConstraints() . When I insert this function into the viewDidLoad() and run my app the constraints are properly set. However when I move the function call into the override func updateViewConstraints() (which is supposed to be called after the subviews have been layed out) no constraints are set. What is the cause of this?

This is what the simple updateViewConstraints() looks like:

 override func updateViewConstraints() {
    super.updateViewConstraints()

    // Add custom view sizing constraints here
    setButtonConstraints()

}

Per Apple's documentation, updateViewConstraints() only gets called if the constraints need to be updated. Also, I believe you need to call the super.updateViewConstraints() after you change constraints.

The following link is helpful.

Where should I be setting autolayout constraints when creating views programmatically

Put your constraints code in updateViewConstraints and call this from viewWillAppear. The same method can also be called in response to rotation. The accepted answer is incorrect on both points it makes. Call super anywhere in the method. If you want to update the constraints call setNeedsUpdateConstraints which will handle it efficiently.

From the docs

You may override this method in a subclass in order to add constraints to the view or its subviews. If you override this method, your implementation must invoke super's implementation.

And here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM