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.
But when I do the same operation in a xib file, this option disappears.
So, is there any way to add this kind of constraints in xib files? Or do I have to add them with code?
From iOS 9 you can also do it more simple with topLayoutGuide's
anchors
:view.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true
[controller.view.topAnchor constraintEqualToAnchor:controller.topLayoutGuide.bottomAnchor].active = YES;
You should refer the following example, this will definitely help you for your problem. I got this from http://developer.apple.com .
Of course, You can not only add constraint between a view and the
top layout guide
orbottom layout guide
by programmatically but also you can remove and access constraint between a view and thetop and bottom layout guide
with the help of KVConstraintExtensionsMaster library.Here is my alternative solution.
Find a UIView as a pivot, set its top layout constraint a fixed vertical space to container's top.
Control-Drag this layout constraint as an IBOutlet, such as
Finally, just override viewWillLayoutSubviews method of UIViewController like following
All the other views' top constraint are based this pivot view, all done :)
Until now even using XCode 6 I cannot align controls to top layout guide on .xib files. Instead, I am using an alternative way.
First, on interface builder, I still align controls to top border of viewcontroler's view.
Then, in viewDidLoad method, I replace some constraints so they will align to top layout guide instead of main view:
Think this is not the best way because we replace objects that we define on interface builder. But it may be an alternative way that we can think about.
I think the reason they don't show up in the XIB is that there is no knowledge of the view controller hierarchy in that situation, whereas in a story board IB can tell where the guides are from the view controller hierarchy the view is in. I.e If it is contained in a UINavigationController, it can determine that the top layout guide is below the navigation toolbar.