Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Layout with navigation bar and view controller (iOS 7)

I'm currently transiting my application to iOS 7 (I want it to remain iOS 6 compatible). This question is not covered by the Apple NDA, it is a question about Auto Layout (it seems that iOS 7 forces Auto Layout (EDIT : was wrong, it is not forced)).

I have a navigation controller with a root view controller (obvious). With iOS 6, i was not using Auto Layout, so the root view controllers was below the navigation bar. With iOS 7, the frame origin does not include the navigation bar, so the top part of my content is hidden...

Have you an idea how to make the entire view above the navigation bar with Auto Layout ?

Thanks !

like image 388
Yohann Prigent Avatar asked Sep 04 '13 00:09

Yohann Prigent


1 Answers

On iOS 7 you have the topLayoutGuide that specify the navigation bar. You can then specify that you want that the constraint of the tableview is on the topLayoutGuide and not the superview.

This will help you to know if it's iOS7 or not:

if ([self respondsToSelector:@selector(topLayoutGuide)])

So it can be something like that

NSString *verticalConstraint = @"V:|[v]|";
NSMutableDictionary *views = [NSMutableDictionary new];
views[@"v"] = self.tableview;
if ([self respondsToSelector:@selector(topLayoutGuide)]) {
    views[@"topLayoutGuide"] = self.topLayoutGuide;
    verticalConstraint = @"V:[topLayoutGuide][v]|";
}
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraint options:0 metrics:nil views:views]];
[self.view addConstraints:constraints];
like image 99
kschaeffler Avatar answered Oct 28 '22 12:10

kschaeffler