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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18603794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:58:26  来源:igfitidea点击:

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

iphoneiosobjective-c

提问by Yohann Prigent

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)).

我目前正在将我的应用程序转换到 iOS 7(我希望它保持与 iOS 6 兼容)。Apple 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. With iOS 7, the frame origin does not include the navigation bar, so the top part of my content is hidden...

我有一个带有根视图控制器的导航控制器(很明显)。在 iOS 6 中,我没有使用自动布局,因此根视图控制器位于导航栏下方。在 iOS 7 中,框架原点不包括导航栏,所以我的内容的顶部被隐藏了......

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

您知道如何使用自动布局在导航栏上方制作整个视图吗?

Thanks !

谢谢 !

回答by kschaeffler

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.

在 iOS 7 上,您有指定导航栏的 topLayoutGuide。然后,您可以指定希望 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];