在 iOS 7 中,状态栏和导航栏出现在我的视图边界上

Status bar and navigation bar appear over my view#39;s bounds in iOS 7(在 iOS 7 中,状态栏和导航栏出现在我的视图边界上)
本文介绍了在 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}}

{{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?

更新:

我已经找到了解决这个特定问题的方法.将导航栏的 translucent 属性设置为 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)每次都会改变它的边界 y 原点.

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

使用视图控制器

在 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 类型,它指定了矩形的四个边中的每一个,除了指定无和全部.使用 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.)

  • 自动调整ScrollViewInsets

如果您不希望滚动视图的内容插入自动进行调整后,将 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 属性表示视图控制器视图中顶部或底部条边的位置.如果条应该与视图的顶部或底部重叠,您可以使用Interface Builder 通过创建相对于栏来定位视图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 中,状态栏和导航栏出现在我的视图边界上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)