Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In iOS 7, we can now add a constraint between a view and the top layout guide, which I think is very useful to solve the status bar offset issue in iOS7(especially when there is no navigation bar in the view).

In a storyboard file, I can add this kind of constraints easily. Just hold the control key then drag the view to the container, it will show the "Top Space to Top Layout Guide" option.

enter image description here

But when I do the same operation in a xib file, this option disappears.

enter image description here

So, is there any way to add this kind of constraints in xib files? Or do I have to add them with code?

like image 642
Henry H Miao Avatar asked Sep 29 '13 12:09

Henry H Miao


People also ask

How do I add constraints in Xib?

Select the options for aligning the selected views, and click the Add Constraints button. Interface Builder creates the constraints needed to ensure those alignments.

How do I add a layout constraint in Xcode?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.

How do you add a safe area to a storyboard?

Select View Controller -> File Inspector (first tab) -> Use safe area layout guides (uncheck the checkbox). You can also change it programmatically in viewDidLoad(). Hope this Helps!


1 Answers

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] ]; 
like image 98
Neeraj Khede Avatar answered Sep 22 '22 06:09

Neeraj Khede