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
49
votes
9 answers

AutoLayout to keep view sizes proportional

I'm trying to achieve the following: I have 2 views in my xib that need to stay 20 pixels off the edge (both sides and top) The 2 views that need to resize aren't the same size They have to be 20 pixels apart Their width needs to stay relative to…
Simon Germain
  • 6,834
  • 1
  • 27
  • 42
48
votes
3 answers

iOS change auto layout constraints when device rotates

I want to modify the layout constraints when the device rotates. My UIViewController is composed of 2 UIViews, in landscape they are horizontally aligned, and in portrait they are vertically aligned. It does work actually, in…
vitaminwater
  • 6,934
  • 3
  • 19
  • 23
44
votes
8 answers

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

I have a View which has two labels and a Table View inside it. I want label 1 to always stay above my Table View and label 2, to be below the Table View. The problem is that the Table View needs to auto-size meaning either increase in height or…
Dumpen
  • 1,622
  • 6
  • 22
  • 36
38
votes
1 answer

Update the constant property of a constraint programmatically in Swift?

I want to animate an object, so I declare a constraint and add it to the view. I then update the constant property of the constraint inside an UIView animation. Why doesn't this code move the object? UIView.animateWithDuration(1, animations: { …
Cesare
  • 9,139
  • 16
  • 78
  • 130
36
votes
4 answers

UITextView inside UIScrollView with AutoLayout

I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is, I placed UIScrollView inside the main view in Storyboard I placed UITextView inside UIScrollView in Storyboard and disabled Scrolling Enabled I…
35
votes
3 answers

Enable + Disable Auto-Layout Constraints

I have a simple (I think) problem: I have a UIImageView, which I have set multiple constraints for in the storyboard. At times, I'll need to disable the constraints, and set its frame manually, but later, I'll want to re-enable those constraints and…
Randoms
  • 2,110
  • 2
  • 20
  • 31
33
votes
4 answers

Hiding a UIView using AutoLayout constraints

From time to time I have a subview that I would like to remove from a layout. Not only should it be hidden, but it should not be considered part of the view's 'flow', so to speak. An example: I am looking for a strategy to hide the orange view…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
33
votes
5 answers

NSLayoutConstraints and setting the width/height of a view dynamically

I have a question about setting the size of a view to which i'm applying some layout constraints. How can I define the size of the view without defining its frame? I'm trying to create a view that has its height fixed and has an origin at the screen…
Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90
32
votes
3 answers

Understanding multiplier in auto layout to use relative positioning

I am trying to understand how one can utilize Auto Layout to position items relative to other views percentage-wise. For example, I recently learned that you can specify a view's bottom should lie 4% higher than its superview's bottom by using…
Jordan H
  • 52,571
  • 37
  • 201
  • 351
32
votes
6 answers

Building a titleView programmatically with constraints (or generally constructing a view with constraints)

I'm trying to build a titleView with constraints that looks like this: I know how I would do this with frames. I would calculate the width of the text, the width of the image, create a view with that width/height to contain both, then add both as…
Bob Spryn
  • 17,742
  • 12
  • 68
  • 91
31
votes
2 answers

How can I rearrange views when autorotating with autolayout?

I frequently run into this problem where I want my views arranged in one manner for portrait and a somewhat drastically different manner for landscape. For a simplified example, consider the following: Portrait: Landscape: Notice how in portrait…
nhgrif
  • 61,578
  • 25
  • 134
  • 173
29
votes
3 answers

Handling AutoLayout constraint animation differences in iOS 10?

I've noticed that in iOS 10 Beta 5 (about to try Beta 6), AutoLayout constraint animation behaves a bit differently. For example, this approach does not work the same as it did in previous iOS releases: [view…
Ben Guild
  • 4,881
  • 7
  • 34
  • 60
29
votes
5 answers

How to add Equal Spacing and equal width for button in iOS Auto layout

Am new to Auto layout constraints in Xcode. I have one Bottom View like UITabBar with 6 UIButtons. Without constraints I aligned those buttons with 5 space padding with each buttons and each buttons has 50 width. Now,I am trying to make it by using…
Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
29
votes
3 answers

activateConstraints: and deactivateConstraints: not persisting after rotation for constraints created in IB

The new NSLayoutConstraint methods activateConstraints: and deactivateConstraints: don't appear to work correctly with IB-created constraints (they do work correctly for code-created constraints). I created a simple test app with one button that…
rdelmar
  • 103,982
  • 12
  • 207
  • 218
28
votes
3 answers

How to set UILabel only width and height and constraints programmatically

I want to create a UILabel programmatically with height, width and then I want to add constraints to it also programmatically for positioning the UILabel. Update: I want to create UI like this: How to create this UI All programatically Code to…
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84