Hokuang Hokuang - 1 year ago 83
iOS Question

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?

Answer Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download