本文共 8400 字,大约阅读时间需要 28 分钟。
本文翻译自:
I recently downloaded 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. 我最近下载了 5 DP,以在iOS 7中测试我的应用程序。我注意到并确认的第一件事是,视图的边界并不一定总是调整为适应状态栏和导航栏的大小。
In viewDidLayoutSubviews
, I print the view's bounds: 在viewDidLayoutSubviews
,我打印视图的边界:
{ {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? 如何解决此问题?
Update: 更新:
I've found a solution for this specific problem. 我已经找到了针对此特定问题的解决方案。 Set the navigation bar's translucent property to NO: 将导航栏的半透明属性设置为NO:
self.navigationController.navigationBar.translucent = NO;
This will fix the view from being framed underneath the navigation bar and status bar. 这样可以防止视图被框在导航栏和状态栏的下面。
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. 第一次更改子视图(UIScrollView)的边界。
参考:
If you want the view to have the translucent nav bar (which is kind of nice) you have to setup a contentInset or similar. 如果您希望视图具有半透明的导航栏(这很不错),则必须设置一个contentInset或类似的内容。
Here is how I do it: 这是我的方法:
// Check if we are running on ios7if([[[[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);}
You can achieve this by implementing a new property called edgesForExtendedLayout
in iOS7 SDK. 您可以通过在iOS7 SDK中实现一个称为edgesForExtendedLayout
的新属性来实现。 Please add the following code to achieve this, 请添加以下代码以实现此目的,
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) self.edgesForExtendedLayout = UIRectEdgeNone;
You need to add the above in your -(void)viewDidLoad
method. 您需要在-(void)viewDidLoad
方法中添加以上内容。
iOS 7 brings several changes to how you layout and customize the appearance of your UI . iOS 7对布局和自定义UI外观的方式进行了一些更改。 The changes in view-controller layout, tint color, and font affect all the UIKit objects in your app. 视图控制器布局,色调颜色和字体的更改会影响应用程序中的所有UIKit对象。 In addition, enhancements to gesture recognizer APIs give you finer grained control over gesture interactions. 此外,手势识别器API的增强功能使您可以更精细地控制手势交互。
Using View Controllers 使用视图控制器
In iOS 7, view controllers use full-screen layout. 在iOS 7中,视图控制器使用全屏布局。 At the same time, iOS 7 gives you more granular control over the way a view controller lays out its views. 同时,iOS 7使您可以更精细地控制视图控制器对视图进行布局的方式。 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 specifywantsFullScreenLayout = NO
, the view controller may display its content at an unexpected screen location when it runs in iOS 7.wantsFullScreenLayout
视图控制器属性在iOS 7中已弃用。如果当前指定了wantsFullScreenLayout = NO
,则视图控制器在iOS 7中运行时可能会在意外的屏幕位置显示其内容。To adjust how a view controller lays out its views,
UIViewController
provides the following properties: 为了调整视图控制器对视图的UIViewController
,UIViewController
提供了以下属性:
- edgesForExtendedLayout edgeForExtendedLayout
The
edgesForExtendedLayout
property uses theUIRectEdge
type, which specifies each of a rectangle's four edges, in addition to specifying none and all.edgesForExtendedLayout
属性使用UIRectEdge
类型,该类型除了指定无和全部外,还指定矩形的四个边缘中的每一个。 UseedgesForExtendedLayout
to specify which edges of a view should be extended, regardless of bar translucency. 使用edgesForExtendedLayout
可以指定应扩展视图的哪些边缘,而不考虑条形的透明度。 By default, the value of this property isUIRectEdgeAll
. 默认情况下,此属性的值为UIRectEdgeAll
。
- extendedLayoutIncludesOpaqueBars extendedLayoutIncludesOpaqueBars
If your design uses opaque bars, refine
edgesForExtendedLayout
by also setting theextendedLayoutIncludesOpaqueBars
property to NO . 如果您的设计使用不透明的条,edgesForExtendedLayout
还可以通过将extendedLayoutIncludesOpaqueBars
属性设置为NO来优化edgesForExtendedLayout
。 (The default value ofextendedLayoutIncludesOpaqueBars
is NO .) (extendedLayoutIncludesOpaqueBars
的默认值为NO 。)
- automaticallyAdjustsScrollViewInsets 自动调整ScrollViewInsets
If you don't want a scroll view's content insets to be automatically adjusted, set
automaticallyAdjustsScrollViewInsets
to NO . 如果不希望滚动视图的内容插图进行自动调整吗,设置automaticallyAdjustsScrollViewInsets
为NO。 (The default value ofautomaticallyAdjustsScrollViewInsets
is YES .) (automaticallyAdjustsScrollViewInsets
的默认值为YES 。)
- topLayoutGuide, bottomLayoutGuide topLayoutGuide,bottomLayoutGuide
The
topLayoutGuide
andbottomLayoutGuide
properties indicate the location of the top or bottom bar edges in a view controller's view.topLayoutGuide
和bottomLayoutGuide
属性指示视图控制器视图中顶部或底部条边缘的位置。 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 oftopLayoutGuide
or to the top of bottomLayoutGuide. 如果条形图应与视图的顶部或底部重叠,则可以通过在topLayoutGuide
的底部或topLayoutGuide
的顶部创建约束,使用Interface Builder将视图相对于条形图放置。 (If no bars should overlap the view, the bottom oftopLayoutGuide
is the same as the top of the view and the top ofbottomLayoutGuide
is the same as the bottom of the view.) Both properties are lazily created when requested. (如果没有条应该重叠视图,底部topLayoutGuide
是一样的视图的顶部和顶部bottomLayoutGuide
是一样的视图的底部。)请求时延后创建这两个属性。
Please refer, 请参考,
You don't have to calculate how far to shift everything down, there's a build in property for this. 您不必计算将所有内容向下移动的距离,它有一个内置属性。 In Interface Builder, highlight your view controller, and then navigate to the attributes inspector. 在Interface Builder中,突出显示视图控制器,然后导航到属性检查器。 Here you'll see some check boxes next to the words "Extend Edges". 在这里,您会在“扩展边缘”一词旁边看到一些复选框。 As you can see, in the first screenshot, the default selection is for content to appear under top and bottom bars, but not under opaque bars, which is why setting the bar style to not translucent worked for you. 如您所见,在第一个屏幕截图中,默认选择是将内容显示在顶部和底部栏的下方,而不是不透明栏的下方,这就是将栏样式设置为不透明的原因。
As you can somewhat see in the first screenshot, there are two UI elements hiding below the navigation bar. 如您在第一个屏幕截图中所看到的,导航栏下方隐藏了两个UI元素。 (I've enabled wireframes in IB to illustrate this) These elements, a UIButton and a UISegmentedControl both have their "y" origin set to zero, and the view controller is set to allow content below the top bar. (我已经在IB中启用了线框来说明这一点),这些元素(一个UIButton和一个UISegmentedControl都将其“ y”原点设置为零,并且将视图控制器设置为允许顶部栏下方的内容)。
This second screenshot shows what happens when you deselect the "Under Top Bars" check box. 第二张屏幕截图显示了取消选中“顶部栏下方”复选框时发生的情况。 As you can see, the view controllers view has been shifted down appropriately for its y origin to be right underneath the navigation bar. 如您所见,视图控制器视图已适当下移,以使其y原点位于导航栏的正下方。
This can also be accomplished programmatically through the usage of -[UIViewController edgesForExtendedLayout]
. 也可以通过使用-[UIViewController edgesForExtendedLayout]
以编程方式完成。 Here's a link to the class reference for , and for 这是和的类引用的链接。
[self setEdgesForExtendedLayout:UIRectEdgeNone];
在您的应用程序plist文件中添加一行,将其称为“基于视图控制器的状态栏外观”并将其设置为NO 。
The simplest trick is to open the file and do these two simple steps: 最简单的技巧是打开文件并执行以下两个简单步骤:
And the result : 结果 :
转载地址:http://dfexb.baihongyu.com/