UIView: layoutIfNeeded() method
The layoutIfNeeded() method immediately performs any pending layout for this view and its descendants.
Syntax
layoutIfNeeded()Parameters
None.
Return value
None (undefined).
Exceptions
None.
Description
- Constraints don't take effect immediately. The system computes them once, after the current JS finishes running and before the next screen refresh. Call this method before reading
frameright after activating constraints or changingconstant. - Call it on a common ancestor. Calling it on the view controller's root view
page.viewis recommended, so that theframeof every view in the view controller is up to date. - Code that only sets up the interface and doesn't read
frameafterward doesn't need it. - Calling it inside the
animationsofUIView.animateWithDuration()applies constraint changes with animation.
Examples
js
const page = new UIView();
const banner = new UIView();
banner.translatesAutoresizingMaskIntoConstraints = false;
page.addSubview(banner);
NSLayoutConstraint.activateConstraints([
banner.widthAnchor.constraintEqualToConstant(200),
banner.heightAnchor.constraintEqualToConstant(60),
]);
page.layoutIfNeeded();
console.log(banner.frame.width, banner.frame.height);Result: Logs 200 60. Reading frame without calling layoutIfNeeded() returns the result of the previous layout pass.
Corresponding iOS API
-[UIView layoutIfNeeded].