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

Centering view with visual format NSLayoutConstraints

I am trying to center a view using the visual format language. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_progressView(300)]-|" options:NSLayoutFormatAlignAllCenterY metrics:0 views:views]]; (views is a…
Joseph
  • 9,171
  • 8
  • 41
  • 67
22
votes
2 answers

Using Auto Layout, how do I make two labels on the same "line" be dynamic in their widths (adjusting with how wide each one needs to be)?

Say I have two UILabels, one is a URL and one is a timer: [http://website.com/link/to/specific/story] [9m 42s] But the "timer" UILabel changes width a lot. Sometimes it is just 12s for example, other times it is 125h 32m 8s. There's a lot of…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
22
votes
2 answers

How do I create a constraints based view that resizes between iPhone 5 and iPhone 4 comparable to Anchor operation?

The sort of classic Springs and Struts, aka "Anchor and Align", or "Autosizing masks" is the only sort of resize-management I understand. However in XCode 4.6, "autolayout" using constraints as introduced in iOS 6, is the default, and it makes…
Warren P
  • 65,725
  • 40
  • 181
  • 316
22
votes
1 answer

Add constraint not listed in Editor menu in Xcode

I am trying to add layout constraints to my NIB, and i am following the directions apple provides…
Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90
21
votes
4 answers

How to "find" your own constraint?

Say I have a UIView, class CleverView: UIView In the custom class, I want to do this: func changeWidth() { let c = ... find my own layout constraint, for "width" c.constant = 70 * Gameinfo.ImportanceOfEnemyFactor } Similarly I wanna be able…
Fattie
  • 27,874
  • 70
  • 431
  • 719
21
votes
2 answers

_UIButtonBarStackView: breaking constraint when becomeFirstResponder sent

When jumping from one textfield to an other, get this: translatesAutoresizingMaskIntoConstraints) ( "", "
János
  • 32,867
  • 38
  • 193
  • 353
21
votes
3 answers

Programmatically creating constraints bound to view controller margins

I'm trying to make a view that will act as a sort of "panel", attached to the right side of the view controller. That is, it is bound to the trailing, top, and bottom margins of the parent view controller, with a static width of 300 However, I just…
prawn
  • 2,643
  • 2
  • 33
  • 49
21
votes
3 answers

How to change or update NSLayoutConstraint programmatically

I have implemented AutoLayout programmatically using the Code : - (void)addConstraintWithListNavigationViewController:(UIView *)listViewNavigation y:(CGFloat)y height:(CGFloat)height { //WIDTH_ListTableView = 0.4 //set x = 0; …
Nijat2018
  • 863
  • 2
  • 10
  • 26
21
votes
5 answers

How is it possible that UITableViewCellContentView height is different from heightForRowAtIndexPath:

I'm facing a really weird problem on my UITableView. I have some different cells that I want to display on a newsfeed-like table view. And I have a problem with a cell after reuse. The cell contains two images, two labels and a view with the…
Talal MAZROUI
  • 645
  • 1
  • 6
  • 12
20
votes
3 answers

Understanding "firstBaseLine" vs. "baseLine" AutoLayout Constraint

I've created the following view that looks as follow: And used Add missing constraints t automatically add constraints. I am pretty new in iOS world and I'm trying to configure out how constraints work. On document outline, I can see the added…
softshipper
  • 32,463
  • 51
  • 192
  • 400
20
votes
2 answers

What are gray constraints in Xcode view hierarchy debug view?

I know what the solid ones are: I created them myself. What about the gray ones? Implicit, maybe via size-to-fit? The gray height conflicts with the solid height. Where do the gray ones come from?
Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55
20
votes
6 answers

When will (or won't) updateViewConstraints be called on my view controller for me by the framework?

I have a view controller and I want to craete and set my view constraints in updateViewConstraints. I have a break point in that method and it's never getting called. Why might it not be getting called? When will the framework want to call this…
topwik
  • 3,487
  • 8
  • 41
  • 65
19
votes
4 answers

Silence "Views without any layout constraints may clip their content" on a specific view controller that has preferredContentSize set

I have a view controller that's fixed size (it can not be resized). This specific view controller is presented as a sheet in a window, I disabled the resizing by setting preferredContentSize. Since it can't be resized anymore, I left the view…
Tom Shen
  • 1,838
  • 3
  • 19
  • 40
19
votes
2 answers

how can i increase the height of an inputAccessoryView

I have spent several days on this with no solution in sight. I have an inputAccessoryView which consists of a UIView containing a textView and two buttons. The behaviour of the inputAccessoryView is as expected and works fine in all cases except…
19
votes
2 answers

How do I animate a NSLayoutConstraint in Swift?

I would like to animate an UIImageView. I declared a NSLayoutConstraint in viewDidLoad and used this code: UIView.animate(withDuration: 1) { myConstraint.constant = 100 self.view.layoutIfNeeded() } Why doesn't my image move?
Cesare
  • 9,139
  • 16
  • 78
  • 130