Creating Auto Layout constraints to topLayoutGuide and bottomLayoutGuide in code

For a UIButton that you want to place 20 points below the UIViewController.topLayoutGuide you create the NSLayoutConstraint like so: [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20.0]; With iOS 9 you can also create the NSLayoutConstraint this way: [self.button.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor constant:20.0];

iOS 7: Misplaced View Frame for “Label – Label” will be different at run time

The accepted answer will fix the problem, but that’s not a good solution because it’ll remove all your constraints. If you have spent hours on the constraints then don’t do that. If you click the triangle next to the warning you can get the explanation of what is misplaced. Then you can just move the … Read more

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

Unable to simultaneously satisfy constraints – No constraints in place

Let’s look at these one by one. “<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=–& v=–& V:[UIView:0xa330270(768)]>” This is saying view 0xa330270 (A) must be 768 points high. “<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: ‘|’:UIView:0xa330270 )>” This is saying view 0xa331260 (B)’s bottom edge must be a gap of -1 from the bottom of A, which is it’s superview. “<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: ‘|’:UIView:0xa330270 )>” … Read more

Width and Height Equal to its superView using autolayout programmatically?

If someone is looking for a Swift solution – I would create a Swift extension for UIView which will help you each time you want to bind a subviews frame to its superviews bounds: Swift 2: extension UIView { /// Adds constraints to this `UIView` instances `superview` object to make sure this always has the … Read more

“Width equals height” constraint in Interface Builder

Update Xcode 5.1b5 Ctrl+click and drag from a view and release while the pointer is over the view. Select “Aspect Ratio”. It will create a constraint where the first and second item is the view. Before Xcode 5.1 You can’t because the width/height editor lacks the fields to relate to another property or set the … Read more

How can I set aspect ratio constraints programmatically in iOS?

Layout Anchors is the most convenient way to set constraints programmatically. Say you want to set 5:1 aspect ratio for your button then you should use: button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/5.0).isActive = true Here’s the full code: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type: .custom) button.setTitle(“Login”, for: .normal) button.backgroundColor = … Read more

Auto layout constraints issue on iOS7 in UITableViewCell

I had this problem as well.. It appears that the contentView’s frame doesn’t get updated until layoutSubviews is called however the frame of the cell is updated earlier leaving the contentView’s frame set to {0, 0, 320, 44} at the time when the constraints are evaluated. After looking at the contentView in more detail, It … Read more