When should translatesAutoresizingMaskIntoConstraints be set to true?

translatesAutoresizingMaskIntoConstraints needs to be set to false when: You Create a UIView-based object in code (Storyboard/NIB will set it for you if the file has autolayout enabled), And you want to use auto layout for this view rather than frame-based layout, And the view will be added to a view hierarchy that is using auto … Read more

Set UITableView’s height to the height of its content with Auto Layout

You have to override updateViewConstraints() in your UIViewController and set the height constraint’s constant to tableView.contentSize.height: override func updateViewConstraints() { tableHeightConstraint.constant = tableView.contentSize.height super.updateViewConstraints() } Then you have to make sure that Label2 has a top constraint that is greaterThanOrEqual to the table view’s bottom. And you also have to change the table view’s height … Read more

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

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

Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

I just built this Category (https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints) that update constrains when you want: //you can use tools to hide/show a uiview [myView1 hideByHeight:YES]; Or just do it to hide an UIView with autolayout: //Hide View [myView1 setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];