Questions tagged [nslayoutconstraint]

NSLayoutConstraint is a constraint defines a relationship between two of user interface objects that must be satisfied by the constraint-based layout system. Available in iOS 6.0 and later in UIKit and available in OS X v10.7 and later in AppKit.

A constraint defines a relationship between two attributes of user interface objects that must be satisfied by the constraint-based layout system.

The relationship involves a first attribute, a relationship type, and a modified second value formed by multiplying an attribute by a constant factor and then adding another constant factor to it. In other words, constraints look very much like linear equations of the following form:

attribute1 == multipler × attribute2 + constant

The right side of this equation can be considered a modified attribute, so this equation could be described in words as “attribute one must be equal to the modified attribute two.” Note that the constraint-based layout system is free to modify the attributes on either side of the equation as part of satisfying the system of constraints that apply to the objects being laid out.

Constraints are not limited to equality relationships. They can also use greater than or equal (>=) or less than or equal (<=) to describe the relationship between the two attributes. Constraints also have priorities, indicating to the layout system which constraint should take priority when two or more constraints are in conflict. The combination of a set of constraints, along with the set of priorities for applying those constraints, can uniquely describe the relationship between multiple user interface elements in all legal configurations for those elements, allowing the system to dynamically layout the user interface as the size and location of user interface elements change.

Source: Apple IOS Developer Library (NSLayoutConstraint class reference)

2151 questions
28
votes
1 answer

What is the difference between all these Auto Layout update methods? Are all necessary?

In the code below, these four methods are called for layout reasoning. I'm a little confused why all of them are needed, though, and what they do differently from one another. They're used in the process to make a cell's height be dynamic with Auto…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
26
votes
1 answer

Install/Uninstall constraint programmatically

Is there a way to install and uninstall an Xcode interface builder constraint programmatically?
Eric
  • 3,811
  • 5
  • 15
  • 18
26
votes
4 answers

Setting constraints programmatically

I'm experimenting with how to use UIScrollView. After much trouble, I finally got the hang of it. But now I've seem to hit another snag. In this simple app, I have a scroll view with and in order for it to work, I have to set the view's bottom space…
Isuru
  • 30,617
  • 60
  • 187
  • 303
25
votes
4 answers

how can I use one storyboard for 4" and 3.5" iphone screens with autolayout (ios6 + ios7)?

While there are many questions and answers about building a storyboard layout that would work both on 4" and 3.5" screen sizes, I couldn't find a suitable solution for the following scenario. I use autolayout with ios6 (and ios7), and I don't have…
gardenofwine
  • 1,344
  • 2
  • 15
  • 24
25
votes
3 answers

How do you make Interface Builder respect a custom view's intrinsic content size in constraint based layout?

Interface Builder in XCode 4.5 respects the intrinsicContentSize for some views, e.g. NSButton, but I can't convince it to respect it on my own custom subviews. This causes IB to add extra constraints trying to force the layout drawn in IB, which…
bitmusher
  • 877
  • 1
  • 7
  • 16
24
votes
5 answers

Unable to make outlet connection to a constraint in IB

I am running XCode 5 with iOS 7. I need to dynamically change the height constraint of a UITextView prior to its layout. I have seen many postings stating that this can be done by making an outlet connection from the constraint in IB to the source…
johnnyspo
  • 619
  • 3
  • 7
  • 18
23
votes
2 answers

UIToolbar with UIBarButtonItem LayoutConstraint issue

I'm creating a UIToolbar with UIBarButtonItem in it programatically. I'm not using .xib or storyboard for this ViewController. Here's the code of how I created it. NSMutableArray *items = [[NSMutableArray alloc] init]; UIBarButtonItem…
23
votes
3 answers

Why calling setNeedsUpdateConstraints isn't needed for constraint changes or animations?

Readings: From this answer: This is what the accepted answer suggests to animate your view changes: _addBannerDistanceFromBottomConstraint.constant = 0 UIView.animate(withDuration: 5) { self.view.layoutIfNeeded() } Why do we call…
mfaani
  • 33,269
  • 19
  • 164
  • 293
23
votes
4 answers

How to change Auto Layout constraints after they are set when using constraintEqualToAnchor()?

I try to set up a view with AutoLayout constraints by using constraintEqualToAnchor(): override func viewDidLoad() { super.viewDidLoad() let myView = UIView() myView.backgroundColor = UIColor.orangeColor() …
Joe Huang
  • 6,296
  • 7
  • 48
  • 81
23
votes
5 answers

NSLayoutConstraint constant not updating after setting

I have a UIView subclass with a corresponding xib file. In my xib I have a NSLayoutConstraint property which I'm trying to animate. I have an animateIn method. The problem is that only the animateIn method works. When I try to update the constant…
Ramin Afshar
  • 989
  • 2
  • 18
  • 34
23
votes
1 answer

Where is the Xcode checkbox "Translates Autoresizing Mask Into Constraints"

In one of the WWDC 2012 videos, Auto Layout By Example, they demo an OS X app using Autolayout, and at about 7 or 8 minutes in, he shows how for a single view, you can uncheck a box in the Attributes Inspector, and the box is called something like…
bobsmells
  • 1,359
  • 2
  • 11
  • 22
22
votes
6 answers

Get reference to NSLayoutConstraint using Identifier set in storyboard

I was setting the constraints of a button using a storyboard. I saw an option, "Identifier" in the constraint's properties. I want to make a reference to this constraint, to change its value in code, to move an object. How can I get a reference to…
Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
22
votes
9 answers

Using autolayout in a tableHeaderView

I have a UIView subclass that contains a multi-line UILabel. This view uses autolayout. I would like to set this view as the tableHeaderView of a UITableView (not a section header). The height of this header will depend on the text of the label,…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
22
votes
2 answers

Cannot add new alignment constraints auto layout Xcode 6

Why I cannot add new alignment constraints on my scroll view ?
cmii
  • 3,556
  • 8
  • 38
  • 69
22
votes
8 answers

iOS - Find top constraint for a view?

I am trying to find the top constraint of the view in code. The top constraint is added in storyboard, and I don't want to use an IBOutlet. Logging the value of the firstAttribute in the following code seems to always return a constraint of type…
aryaxt
  • 76,198
  • 92
  • 293
  • 442