我最近下载了Xcode 5 DP,在iOS 7上测试我的应用。我注意到并确认的第一件事是,视图的边界并不总是根据状态栏和导航栏调整大小。

在viewDidLayoutSubviews中,我打印了视图的边界:

{{0, 0}, {320, 568}}

这导致我的内容出现在导航栏和状态栏的下方。

我知道我可以通过获取主屏幕的高度,减去状态栏的高度和导航栏的高度来解释高度,但这似乎是不必要的额外工作。

我该如何解决这个问题?

更新:

我已经找到了解决这个问题的方法。将导航栏的半透明属性设置为NO:

self.navigationController.navigationBar.translucent = NO;

这将修复视图被框在导航栏和状态栏下面的问题。

然而,我还没有找到一个修复的情况下,当你想导航栏是半透明的。例如,在全屏查看一张照片时,我希望导航栏是半透明的,视图被框在它下面。这是可行的,但是当我切换显示/隐藏导航栏时,我经历了更奇怪的结果。第一个子视图(一个UIScrollView)每次都会改变它的边界y原点。


当前回答

如果你想让视图有半透明的导航栏(这很好),你必须设置一个contentInset或类似的。

我是这样做的:

// Check if we are running on ios7
if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
      CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
      float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;

      myContentView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
}

其他回答

你可以通过在iOS7 SDK中实现一个名为edgesForExtendedLayout的新属性来实现这一点。请添加以下代码来实现此功能,

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

你需要在你的-(void)viewDidLoad方法中添加上面的内容。

iOS 7 brings several changes to how you layout and customize the appearance of your UI. The changes in view-controller layout, tint color, and font affect all the UIKit objects in your app. In addition, enhancements to gesture recognizer APIs give you finer grained control over gesture interactions. Using View Controllers In iOS 7, view controllers use full-screen layout. At the same time, iOS 7 gives you more granular control over the way a view controller lays out its views. In particular, the concept of full-screen layout has been refined to let a view controller specify the layout of each edge of its view. The wantsFullScreenLayout view controller property is deprecated in iOS 7. If you currently specify wantsFullScreenLayout = NO, the view controller may display its content at an unexpected screen location when it runs in iOS 7. To adjust how a view controller lays out its views, UIViewController provides the following properties: edgesForExtendedLayout The edgesForExtendedLayout property uses the UIRectEdge type, which specifies each of a rectangle’s four edges, in addition to specifying none and all. Use edgesForExtendedLayout to specify which edges of a view should be extended, regardless of bar translucency. By default, the value of this property is UIRectEdgeAll. extendedLayoutIncludesOpaqueBars If your design uses opaque bars, refine edgesForExtendedLayout by also setting the extendedLayoutIncludesOpaqueBars property to NO. (The default value of extendedLayoutIncludesOpaqueBars is NO.) automaticallyAdjustsScrollViewInsets If you don’t want a scroll view’s content insets to be automatically adjusted, set automaticallyAdjustsScrollViewInsets to NO. (The default value of automaticallyAdjustsScrollViewInsets is YES.) topLayoutGuide, bottomLayoutGuide The topLayoutGuide and bottomLayoutGuide properties indicate the location of the top or bottom bar edges in a view controller’s view. If bars should overlap the top or bottom of a view, you can use Interface Builder to position the view relative to the bar by creating constraints to the bottom of topLayoutGuide or to the top of bottomLayoutGuide. (If no bars should overlap the view, the bottom of topLayoutGuide is the same as the top of the view and the top of bottomLayoutGuide is the same as the bottom of the view.) Both properties are lazily created when requested.

请参考,苹果医生

斯威夫特3

override func viewWillAppear(_ animated: Bool) {
    self.edgesForExtendedLayout = []
}

从下拉列表中添加“基于视图控制器的状态栏外观”作为info.plist中的一行。就像这样:

我在ipad (armv7, armv7s, amr64)上的应用程序也有同样的问题,只是通过呈现另一个UIViewController,并在驳回它们后,在状态栏下进入导航栏… 我花了很多时间来寻找解决方案。我使用故事板和在InterfaceBuilder的UIViewController,这使得糟糕的我设置从全屏->当前上下文的演示,它修复了这个问题。它在我的应用程序只适用于ipad => iOS8.0(测试与iOS8.1)和ipad与iOS 7.1不工作!!

只要在视图中设置以下代码就会出现。

  if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
 }