简体   繁体   English

带导航栏和视图控制器的自动布局(iOS 7)

[英]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). 我目前正在将我的应用程序转移到iOS 7(我希望它保持iOS 6兼容)。 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)). 苹果NDA没有涵盖这个问题,这是一个关于自动布局的问题(似乎iOS 7强制自动布局(编辑:错了,它不是强制的))。

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. 在iOS 6中,我没有使用自动布局,因此根视图控制器位于导航栏下方。 With iOS 7, the frame origin does not include the navigation bar, so the top part of my content is hidden... 在iOS 7中,框架原点不包含导航栏,因此隐藏了我内容的顶部...

Have you an idea how to make the entire view above the navigation bar with Auto Layout ? 您是否了解如何使用自动布局在导航栏上方创建整个视图?

Thanks ! 谢谢 !

On iOS 7 you have the topLayoutGuide that specify the navigation bar. 在iOS 7上,您有topLayoutGuide指定导航栏。 You can then specify that you want that the constraint of the tableview is on the topLayoutGuide and not the superview. 然后,您可以指定您希望tableview的约束位于topLayoutGuide而不是superview。

This will help you to know if it's iOS7 or not: 这将帮助您了解它是否是iOS7:

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM