Autolayout: Add Constraint to Superview and Not Top Layout Guide

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

There is an arrow to the right of the constraint constant value. If you click on it, a menu pops out where you can choose what is it that you want to make your constraint relative to. If you choose 'View', than it will pin it to the top of the view.

Sample Image

How can I add a contraint to the root view instead of TopLayoutGuide?


self.automaticallyAdjustsScrollViewInsets = true

The default value of this property is true, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to false if your view controller implementation manages its own scroll view inset adjustments.

You can also set this property from Interface Builder

Is there any way to add constraint between a view and the top layout guide in a xib file?

You should refer the following example, this will definitely help you for your problem. I got this from http://developer.apple.com .

[button setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = myViewController.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);

[myViewController.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-20-[button]"
options: 0
metrics: nil
views: viewsDictionary]
];

Weird behaviour with constraints to top / bottom layout guide

So I found a hint here:

https://www.youtube.com/watch?v=lxc9DnqpI0c

And anyway the trick is to NOT constraint a subview to the top / bottom layout guides, but rather to the superview.

Basically if you want to set a constraint between a subview (not the main view) to the top / bottom of the screen, do not use the usual ctrl+drag technique, but just select it and then go:

Editor > Pin > Top Space to Superview

Spent some few hours on this!

constrain view.bottom to superview.bottom not bottomLayoutGuide in storyboard

Select your TableView and go to Editor -> Pin.

From there you have 4 options to pin to superview:

  • Leading Space to Superview
  • Trailing Space to Superview
  • Top Space to Superview
  • Bottom Space to Superview

Sample Image

This works as expected. The only downside is that you have to perform the pinning manually, as there are no shortcuts available.

How to set layout constraints with top layout guide?

| refers to the superview, so you are creating a constraint that is pinning the view to the top of the superview.

You need to add the top layout guide as an item in your VFL string:

id guide = self.topLayoutGuide;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[guide][button]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(guide,button)]];

Add spacing constraint to superview in Xcode 5

To add a constraint between a view and its' superview (like "bottom space to superview") you can use the Document Outline (The left part of the Interface Builder).

In the Document Outline:

  1. Hold the ctrl-key and drag from the view to its' superview (or vice versa).
  2. Select the constraints you want to add (in this example "Bottom Space to Container").

ctrl-drag from the view to its' superview (or vice versa)
Select the constraints you want to add

iOS 7 AutoLayout Top Space to Top Layout Guide Without Control-Drag in Interface Builder?

You can use the "nearest neighbour" popover in the bottom. Select the top line and click "Add 1 constraint".

This will default your constraint to the top layout guide as shown in the second screenshot below.

Sample Image

Sample Image

equal distance from top layout guide and bottom layout guide

Add another UIView with constraints with top and bottom layout guide. Then add your view as a subview and align vertically and horizontally.

Sample Image

Sample Image



Related Topics



Leave a reply



Submit