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
96
votes
3 answers

"Width equals height" constraint in Interface Builder

I can't find a way to create a 'square' constraint, meaning 'width equals height' in Interface Builder. I guess it's possible to add such constraint programmatically. Is there something I can do in IB? Maybe I just don't see it? It seems trivial,…
DemoniacDeath
  • 1,778
  • 3
  • 13
  • 25
86
votes
11 answers

Width and Height Equal to its superView using autolayout programmatically?

I've been looking for a lot of snippets in the net and I still can't find the answer to my problem. My question is I have a scrollView(SV) and I want to add a button inside scrollView(SV) programmatically with same width and height of its superview…
Bordz
  • 2,810
  • 4
  • 23
  • 27
83
votes
8 answers

Centering subview's X in autolayout throws "not prepared for the constraint"

I have a custom UIView subclass which is being initialized via a nib. In -awakeFromNib, I'm creating a subview and attempting to center it in its superview. [self setInteralView: [[UIView alloc] init]]; [[self internalView] addConstraint:…
72
votes
8 answers

With Auto Layout, how do I make a UIImageView's size dynamic depending on the image?

I want my UIImageView to grow or shrink depending on the size of what the actual image it's displaying is. But I want it to stay vertically centered and 10pts from the leading edge of the superview. However, if I set these two constraints it…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
71
votes
3 answers

When should translatesAutoresizingMaskIntoConstraints be set to true?

I've read the documentation. But I'm still not sure when I need to not set it to false. In the code below if I set it to false I won't see the header at all. If I leave it as true, then everything is fine. The following in View debug hierarchy will…
mfaani
  • 33,269
  • 19
  • 164
  • 293
71
votes
11 answers

Unable to simultaneously satisfy constraints - No constraints in place

I have gone through and removed every single user constraint yet I am still getting the following error ONLY after I rotate the device. I have absolutely no clue why though. Does anyone have any ideas? 2013-01-14 21:30:31.363 myApp[35869:c07] Unable…
user843337
65
votes
4 answers

Creating Auto Layout constraints to topLayoutGuide and bottomLayoutGuide in code

Apple's documentation on creating Auto Layout constraints between a view and one of the layout guides only shows an example using VFL. Is there any way to create these constraints programmatically without VFL (using NSLayoutConstraint's other API or…
smileyborg
  • 30,197
  • 11
  • 60
  • 73
65
votes
5 answers

iOS 7: Misplaced View Frame for "Label - Label" will be different at run time

I just finished an app on iOS 6 and bought a developer account a week ago so haven't had much time playing with the iOS 7 SDK. Just downloaded the Golden Master version and trying to upgrade my app to be compatible with iOS 7. I received a lot of…
AJ112
  • 5,291
  • 7
  • 45
  • 60
64
votes
12 answers

What is _UITemporaryLayoutWidth and why does it break my constraints?

I have a storyboard with Autolayout and Size Classes. Pretty complicated layout, and unfortunately I can't really pin down how to reproduce the problem in a new project. But the view in question is pinned to the left and right edge of the screen…
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
62
votes
5 answers

Update height constraint programmatically

I am new in auto layout. I have done all of my project from xib file, but now I faced a problem where I have to update an view's height programmatically. I have tried below but now working. [[self view] addConstraint:[NSLayoutConstraint…
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
62
votes
5 answers

UITableView within UIScrollView using autolayout

At the moment, I'm using a UITableView along with other views that are contained in a UIScrollView. I want the UITableView to have its height to be the same as its content height. To complicate things, I'm also inserting / deleting rows to provide…
Matt Delves
  • 1,585
  • 2
  • 13
  • 19
59
votes
6 answers

Animate intrinsicContentSize changes

I have a UIView subclass that draws a circle whose radius changes (with nice bouncy animations). The view is deciding the size of the circle. I want this UIView subclass to change its frame size to match the animated changes to the circle radius,…
Loz
  • 2,198
  • 2
  • 21
  • 22
57
votes
9 answers

UICollectionView autosize height

How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling reloadData and invalidating the layout: self.collectionView.contentSize = CGSizeMake(300,…
52
votes
7 answers

What is a 'UIView-Encapsulated-Layout-Width' constraint?

I keep getting 'Unable to simultaneously satisfy constraints' exceptions (Xcode 5, iOS 7, both device and simulator), where one of the constraints in the list is something like this: "
50
votes
3 answers

What's with Constraints in SwiftUI?

What is happening now with constraints in SwiftUI? Do View types adapt automatically for bigger devices etc. or what should we have to do instead?
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96