iOS7 - 状态栏下的视图 - edgesForExtendedLayout无法正常工作

问题描述:

我有一个去年建成的项目,它使用的是XIB,没有故事板。 XIB不使用自动布局,但它们确实使用了一些自动调整大小。运行iOS7时出现问题,其中所有视图都隐藏在状态栏下。我完全理解这是iOS7的一项新功能,可以预期这一点。但是,所有用于修复它的解决方案都不起作用。我在视图顶部有一个图像,它总是显示在状态栏下面,我没有使用导航栏或类似的东西。

I have a project that was built last year, and it uses XIBs, no storyboards. The XIBs do not use Auto Layout, but they do use some Autosizing. I have an issue when running with iOS7, in which all the views are tucked under the status bar. I fully understand this is a new feature with iOS7, in which this can be expected. However, all of the solutions for fixing it to not do this are not working. I have an image at the top of the view that always shows under the status-bar, and I'm not using nav-bars or anything like that.

我有尝试更新XIB中的Y-delta(它们对视图没有影响),我尝试将 edgesForExtendedLayout 设置为 UIRectEdgeNone (什么都不做),还有很多其他的东西。每次,状态栏显示隐藏在它下面的视图,无论我做什么..这是除非我手动向下移动XIB中的视图以留出状态栏的空间(但该解决方案不起作用,因为它当然,在iOS6中看起来并不正确。。

I have tried updating the Y-deltas in the XIB (they have no effect on the view), I have tried setting the edgesForExtendedLayout to UIRectEdgeNone (does nothing), and a multitude of other things. Every time, the status bar shows with the view tucked under it, no matter what I do.. that is unless I manually move down the view in the XIB to allow room for the status bar (but that solution doesn't work because it doesn't look right in iOS6, of course).

奇怪的是,即使我尝试使用一行代码来破解视图转换,它也不会工作(如下所示):

What's odd is that even when I try a line of code to hack in a view-shift, it doesn't work (like the following):

self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+20, self.view.frame.size.width, self.view.frame.size.height);

..不是说我会选择那种解决方案,但它并不奇怪,它没有'工作(我通常看到不工作的唯一时间是如果自动布局到位,在这种情况下不是这样)。

..Not that I would go with that kind of solution, but it's just odd that it didn't work (the only time I typically see that not work is if Auto Layout is in place, which it's not in this case).

这是一个设计要求,状态栏显示,我只是难以理解为什么我不能将视图设置为iOS7的状态栏下。我已阅读有关该主题的每个Stack Overflow帖子,以及Apple的过渡/指南。再一次,重申一下,我完全理解它应该如何运作以及预期的解决方案应该是什么,但这些似乎都不适用于这个特定的项目。

It is a design requirement that the status-bar shows, and I'm just stumped on why I can't set the view to be under the status bar for iOS7. I have read every single Stack Overflow post on the subject, as well as Apple's transition/guides. Once again, to reiterate, I fully understand how it should function and what the expected solution should be to this, but none of that seems to be working for this particular project.

我是一个经验丰富的iOS开发者,但是这个项目是由另一个团队构建的,所以我不知道XIB文件中是否隐藏了某些内容,plist或者可以胜过上述设置的代码。如果有其他可以查看的信息,或者我可以提供更多信息,请告诉我。

I am an experienced iOS dev, but this project was built by another team, so I don't know if there's something hidden somewhere in the XIB files, plist, or code that could be trumping the above settings. Please let me know if there is something else that can be looked at on this, or more information I can provide.

提前致谢!

如果在Interface Builder中设置iOS 6/7 delta值,请记住在Interface Builder Document上将View as设置为iOS 6,因为它是你要复制的iOS 6布局。然后,仅在iOS 7上使用增量将内容推送到状态栏下方。如果您将查看为设置为iOS 7(默认设置),则增量将在iOS 6上为您提供iOS 7外观。

If you set the iOS 6/7 delta values in Interface Builder, remember to set "View as" to "iOS 6" on the Interface Builder Document, since it is the iOS 6 layout you want to replicate. The deltas will then be used only on iOS 7 to push the content below the status bar. If you leave "View as" set to iOS 7 (the default) the deltas will instead give you the iOS 7 look on iOS 6.

但是,增量不会如果您根据视图框架以编程方式重新定位或调整视图大小,可以帮助您,因为框架不考虑增量。

However, the deltas will not help you if you reposition or resize views programmatically based on the view frame, since the frame does not account for the deltas.

而不是使用增量,最好的解决方案我已发现是在主XIB上启用自动布局,然后在顶部/内容视图上设置顶部空间约束以遵循顶部布局指南。本指南在iOS 7中引入,代表状态栏下方的位置。遗憾的是,当不使用Storyboard时,Interface Builder中不提供该指南,但您可以通过编程方式添加它。

Instead of using the deltas, the best solution I have found is to enable Auto Layout on your main XIB and then set the top space constraint on your top/content view to follow the Top Layout Guide. This guide was introduced in iOS 7 and represents the position below the status bar. Unfortunately the guide is not available in Interface Builder when not using Storyboards, but you can add it programmatically.

我所做的是向superview添加顶部空间约束而不是Interface Builder,并在代码中为此创建了一个插座。然后,在viewDidLoad中,如果topLayoutGuide可用(iOS 7+),请使用Top Layout Guide替换此插座中的约束。

What I did was add a top space constraint to the superview instead in Interface Builder, and created an outlet for this in the code. Then, in viewDidLoad, if the topLayoutGuide is available (iOS 7+), replace the constraint in this outlet with a version using the Top Layout Guide instead.

if ([self respondsToSelector:@selector(topLayoutGuide)]) {
    [self.view removeConstraint:self.containerTopSpaceConstraint];

    self.containerTopSpaceConstraint =
    [NSLayoutConstraint constraintWithItem:self.contentView
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.topLayoutGuide
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1
                                  constant:0];

    [self.view addConstraint:self.containerTopSpaceConstraint];

    [self.view setNeedsUpdateConstraints];
    [self.view layoutIfNeeded];
}