Programmatically Add CenterX/CenterY Constraints

Update for Swift 3/Swift 4: As of iOS 8, you can and should activate your constraints by setting their isActive property to true. This enables the constraints to add themselves to the proper views. You can activate multiple constraints at once by passing an array containing the constraints to NSLayoutConstraint.activate() let label = UILabel(frame: CGRect.zero) … Read more

How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

I would suggest the following: Add a height constraint to your collection view. Set its priority to 999. Set its constant to any value that makes it reasonably visible on the storyboard. Change the bottom equal constraint of the collection view to greater or equal. Connect the height constraint to an outlet. Every time you … Read more

How to update the constant height constraint of a UIView programmatically?

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code. yourHeightConstraintOutlet.constant = someValue yourView.layoutIfNeeded() Method updateConstraints() is an instance method of UIView. It is helpful when you are setting the constraints programmatically. It updates … Read more

Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint

I would recommend to debug and find which constraint is “the one you don’t want”. Suppose you have following issue: Always the problem is how to find following Constraints and Views. There are two solutions how to do this: DEBUG VIEW HIERARCHY (Do not recommend this way) Since you know where to find unexpected constraints … Read more

Can I change multiplier property for NSLayoutConstraint?

Here is an NSLayoutConstraint extension in Swift that makes setting a new multiplier pretty easy: In Swift 3.0+ import UIKit extension NSLayoutConstraint { /** Change multiplier constraint – parameter multiplier: CGFloat – returns: NSLayoutConstraint */ func setMultiplier(multiplier:CGFloat) -> NSLayoutConstraint { NSLayoutConstraint.deactivate([self]) let newConstraint = NSLayoutConstraint( item: firstItem, attribute: firstAttribute, relatedBy: relation, toItem: secondItem, attribute: secondAttribute, … Read more

UITextView that expands to text using auto layout

Summary: Disable scrolling of your text view, and don’t constraint its height. To do this programmatically, put the following code in viewDidLoad: let textView = UITextView(frame: .zero, textContainer: nil) textView.backgroundColor = .yellow // visual debugging textView.isScrollEnabled = false // causes expanding height view.addSubview(textView) // Auto Layout textView.translatesAutoresizingMaskIntoConstraints = false let safeArea = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ textView.topAnchor.constraint(equalTo: … Read more

setNeedsLayout vs. setNeedsUpdateConstraints and layoutIfNeeded vs updateConstraintsIfNeeded

Your conclusions are right. The basic scheme is: setNeedsUpdateConstraints makes sure a future call to updateConstraintsIfNeeded calls updateConstraints. setNeedsLayout makes sure a future call to layoutIfNeeded calls layoutSubviews. When layoutSubviews is called, it also calls updateConstraintsIfNeeded, so calling it manually is rarely needed in my experience. In fact, I have never called it except when … Read more