本文介绍了状态栏和导航栏出现在 iOS 7 中我的视图边界上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了 Xcode 5 DP 来在 iOS 7 中测试我的应用程序.我做的第一件事注意到并确认的是,我的视图边界并不总是调整大小以适应状态栏和导航栏.

I recently downloaded Xcode 5 DP to test my apps in iOS 7. The first thing I noticed and confirmed is that my view's bounds is not always resized to account for the status bar and navigation bar.

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

In viewDidLayoutSubviews, I print the view's bounds:

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

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

This results in my content appearing below the navigation bar and status bar.

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

I know I could account for the height myself by getting the main screen's height, subtracting the status bar's height and navigation bar's height, but that seems like unnecessary extra work.

我该如何解决这个问题?

How can I fix this issue?

更新:

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

I've found a solution for this specific problem. Set the navigation bar's translucent property to NO:

self.navigationController.navigationBar.translucent = NO;

这将修复导航栏和状态栏下方的视图.

This will fix the view from being framed underneath the navigation bar and status bar.

但是,当您希望导航栏半透明时,我还没有找到解决方法.例如,在全屏查看照片时,我希望导航栏是半透明的,并且视图在其下方被框起来.这有效,但是当我切换显示/隐藏导航栏时,我遇到了更奇怪的结果.第一个子视图(一个 UIScrollView)每次都改变它的边界和原点.

However, I have not found a fix for the case when you want the navigation bar to be translucent. For instance, viewing a photo full screen, I wish to have the navigation bar translucent, and the view to be framed underneath it. That works, but when I toggle showing/hiding the navigation bar, I've experienced even stranger results. The first subview (a UIScrollView) gets its bounds y origin changed every time.

推荐答案

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

You can achieve this by implementing a new property called edgesForExtendedLayout in iOS7 SDK. Please add the following code to achieve this,

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

您需要在 -(void)viewDidLoad 方法中添加上述内容.

You need to add the above in your -(void)viewDidLoad method.

iOS 7 为您的布局和自定义方式带来了一些变化UI 的外观.视图控制器布局的变化,色调颜色和字体会影响应用中的所有 UIKit 对象.在此外,手势识别器 API 的增强为您提供了更好的对手势交互的粒度控制.

使用视图控制器

在 iOS 7 中,视图控制器使用全屏布局.同时,iOS 7 让你更精细地控制视图控制器的方式阐述其观点.特别是全屏布局的概念已改进为让视图控制器指定每个的布局其视图的边缘.

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.

wantsFullScreenLayout 视图控制器属性在iOS 7. 如果您当前指定 wantsFullScreenLayout = NO,则视图控制器可能会在意外的屏幕位置显示其内容当它在 iOS 7 中运行时.

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.

要调整视图控制器如何布局其视图,UIViewController提供以下属性:

To adjust how a view controller lays out its views, UIViewController provides the following properties:

  • edgesForExtendedLayout

edgesForExtendedLayout 属性使用 UIRectEdge 类型,它指定了矩形的四个边中的每一个,除了指定 none 和 all.使用 edgesForExtendedLayout 指定哪个无论条形半透明如何,都应扩展视图的边缘.经过默认,该属性的值为UIRectEdgeAll.

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

如果您的设计使用不透明条,则将 edgesForExtendedLayout 细化为还将 extendedLayoutIncludesOpaqueBars 属性设置为.(extendedLayoutIncludesOpaqueBars 的默认值为 NO.)

If your design uses opaque bars, refine edgesForExtendedLayout by also setting the extendedLayoutIncludesOpaqueBars property to NO. (The default value of extendedLayoutIncludesOpaqueBars is NO.)

  • automaticallyAdjustsScrollViewInsets

如果您不希望滚动视图的内容插入自动调整后,将 automaticallyAdjustsScrollViewInsets 设置为 NO.(这automaticallyAdjustsScrollViewInsets 的默认值为 YES.)

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

topLayoutGuidebottomLayoutGuide 属性表示视图控制器视图中顶部或底部栏边缘的位置.如果条形图应与视图的顶部或底部重叠,您可以使用界面生成器通过创建相对于栏定位视图topLayoutGuide 底部或顶部的约束底部布局指南.(如果没有条形应与视图重叠,则底部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.

请参考,苹果文档

这篇关于状态栏和导航栏出现在 iOS 7 中我的视图边界上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:17