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

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

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

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

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

我该如何解决这个问题?

更新:

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

self.navigationController.navigationBar.translucent = NO;

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

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


当前回答

对我来说,最简单的解决方案是在plist中添加两个键

其他回答

你不需要计算要往下平移多远,这有一个内置属性。在Interface Builder中,突出显示视图控制器,然后导航到属性检查器。在这里你会看到一些复选框旁边的文字“扩展边缘”。正如你所看到的,在第一个截图中,默认的选择是让内容出现在顶部和底部的栏下,而不是在不透明的栏下,这就是为什么将栏样式设置为不半透明的原因。

正如你在第一张截图中看到的,有两个UI元素隐藏在导航栏下面。这些元素,一个UIButton和一个UISegmentedControl都有他们的“y”原点设为零,视图控制器被设置为允许内容低于顶部栏。

这第二张截图显示了当你取消选择“顶部栏下”复选框时会发生什么。正如你所看到的,视图控制器视图已经适当地向下移动,因为它的y原点在导航栏的正下方。

这也可以通过使用-[UIViewController edgesForExtendedLayout]来实现。这里有一个edgeForExtendedLayout和UIRectEdge类引用的链接

[self setEdgesForExtendedLayout:UIRectEdgeNone];

我有一个场景,我使用苹果编写的BannerViewController来显示我的广告和嵌入在BannerViewController中的ScrollViewController。

为了防止导航栏隐藏我的内容,我必须做两个更改。

1)修改BannerViewController.m

- (void)viewDidLoad
{
   [super viewDidLoad];
   float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
   if (systemVersion >= 7.0) {
      self.edgesForExtendedLayout = UIRectEdgeNone;
   }
}

2)修改我的scrollviewcontroller

- (void)viewDidLoad
{
    [super viewDidLoad];
    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (systemVersion >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeBottom;
    }
}

现在广告正确地显示在视图的底部,而不是被导航栏覆盖,顶部的内容也没有被切断。

隐藏状态栏的步骤:

1.转到您的应用程序信息。plist文件。

2.和设置,查看基于控制器的状态栏外观:布尔NO

希望我解决了状态栏问题.....

Swift 3 / Swift 4解决方案,也适用于iOS 10+中的nib /XIB文件:

override func viewDidLoad() {
    super.viewDidLoad()

    edgesForExtendedLayout = []
}

你可以通过在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.

请参考,苹果医生