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
566
votes
19 answers

How to add constraints programmatically using Swift

I'm trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically in Swift to a UIView using this code: var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100,…
Sara Canducci
  • 6,231
  • 5
  • 19
  • 24
249
votes
3 answers

setNeedsLayout vs. setNeedsUpdateConstraints and layoutIfNeeded vs updateConstraintsIfNeeded

I know that the auto layout chain consists in basically 3 different process. updating constraints layout views (here is where we get calculation of frames) display What's is not totally clear to me is the inner difference between -setNeedsLayout…
Andrea
  • 26,120
  • 10
  • 85
  • 131
183
votes
17 answers

UITextView that expands to text using auto layout

I have a view that is laid out completely using auto layout programmatically. I have a UITextView in the middle of the view with items above and below it. Everything works fine, but I want to be able to expand UITextView as text is added. This…
tharris
  • 2,192
  • 2
  • 13
  • 14
175
votes
18 answers

Can I change multiplier property for NSLayoutConstraint?

I created two views in one superview, and then added constraints between views: _indicatorConstrainWidth = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view2…
Bimawa
  • 3,535
  • 2
  • 25
  • 45
160
votes
17 answers

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

Below is the error message I receive in the debug area. It runs fine and nothing is wrong except that I receive this error. Would this prevent apple accepting the app? How do I fix it? 2012-07-26 01:58:18.621 Rolo[33597:11303] Unable to…
Johnny Cox
  • 1,873
  • 3
  • 17
  • 17
139
votes
10 answers

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

I have a UIView and I set the constraints using Xcode Interface Builder. Now I need to update that UIView instance's height constant programmatically. There is a function that goes like myUIView.updateConstraints(), but I don't know how to use it.
Chris Mikkelsen
  • 3,987
  • 9
  • 29
  • 41
131
votes
6 answers

What happens with constraints when a view is removed

The question I have is simple but I couldn't find any information in the documentation. What happens with layout constraints when a view is removed from the view hierarchy (or moved to another view)? For example, let's have container C with subviews…
Sulthan
  • 128,090
  • 22
  • 218
  • 270
127
votes
6 answers

iOS: How does one animate to new autolayout constraint (height)

I've never worked with autolayout constraints before. I have a small new app I'm working on and noticed that the NIB's views are defaulting to autolayout. So, I figured I'd take the opportunity to work with it and try to figure out where Apple is…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
125
votes
18 answers

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

I would like the UICollectionView (The red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of empty space. What I tried is to use: override func layoutSubviews() { …
charbinary
  • 1,875
  • 4
  • 17
  • 25
117
votes
8 answers

When can I activate/deactivate layout constraints?

I've set up multiple sets of constraints in IB, and I'd like to programmatically toggle between them depending on some state. There's a constraintsA outlet collection all of which are marked as installed from IB, and a constraintsB outlet collection…
tybro0103
  • 48,327
  • 33
  • 144
  • 170
114
votes
7 answers

Autolayout: Add constraint to superview and not Top Layout Guide?

I have a UIView in my UIViewController in storyboard which I want to add a constraint on to space that view a distance from the top edge. Now, when I do the usual by ctrl + drag to the ViewController's main view I only get option to set it to top…
Arbitur
  • 38,684
  • 22
  • 91
  • 128
108
votes
7 answers

Programmatically Add CenterX/CenterY Constraints

I have a UITableViewController that doesn't display any sections if there is nothing to show. I've added a label to indicate to the user that there is nothing to display with this code: label = UILabel(frame: CGRectMake(20, 20, 250,…
Mario A Guzman
  • 3,483
  • 4
  • 27
  • 36
106
votes
10 answers

Auto layout constraints issue on iOS7 in UITableViewCell

I'm using auto layout constraints programmatically to layout my custom UITableView cells and I'm correctly defining the cell sizes in tableView:heightForRowAtIndexPath: It's working just fine on iOS6 and it does look fine in iOS7 as well BUT when I…
Alexis
  • 16,629
  • 17
  • 62
  • 107
105
votes
1 answer

NSLayoutConstraint crashes ViewController

Possible Duplicate: presentViewController: crash on iOS 6 (AutoLayout) I'm getting this error when clicking on a button in my app: 2012-06-28 21:43:36.860 AppName[2403:707] *** Terminating app due to uncaught exception…
tracifycray
  • 1,423
  • 4
  • 18
  • 29
104
votes
7 answers

How can I set aspect ratio constraints programmatically in iOS?

I have used auto layout for my view controllers. I have set the V and H positions in constraints, but I want to know how can I increase my button size when it changes to 5s, 6 and 6 Plus. This is the way I added constraints for the login…
IRD
  • 1,127
  • 2
  • 8
  • 15
1
2 3
99 100