xcode iOS 7 状态栏和 self.view.frame 和 self.view.bounds

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18909054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:50:22  来源:igfitidea点击:

iOS 7 status bar and self.view.frame and self.view.bounds

xcodeios6uiviewios7statusbar

提问by Mrwolfy

I recently released an iOS6 app, and I need to update it for iOS 7. The new status bar is a bit of an issue. The frame/bounds of self.view seem to have changed (+20 points) and I use self.view.bounds to determine the placement of some elements. I have been looking at some solutions. Basically I need to update the app while still supporting the iOS 6 status bar. Is there a best practice for this?

我最近发布了一个 iOS6 应用程序,我需要为 iOS 7 更新它。新的状态栏有点问题。self.view 的框架/边界似乎发生了变化(+20 分),我使用 self.view.bounds 来确定某些元素的位置。我一直在寻找一些解决方案。基本上我需要更新应用程序,同时仍然支持 iOS 6 状态栏。是否有最佳实践?

The code below seems to work to detect an iOS 7 device and shift the content into position, but also causes other issues. Anyway I am not confident that this is the best way.

下面的代码似乎可以检测 iOS 7 设备并将内容移动到位,但也会导致其他问题。无论如何,我不相信这是最好的方法。

if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
    CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
    float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;

    [myContentView setFrame:CGRectMake(0, heightPadding, self.view.frame.size.width, self.view.frame.size.height - heightPadding)];
}

enter image description here

在此处输入图片说明

回答by Nandha

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

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

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

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

您需要在 -(void)viewDidLoad 方法中添加上述代码。

回答by spstanley

I used this solution to adjust my view in a modal dialog:

我使用此解决方案在模式对话框中调整我的视图:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        CGRect sbFrame = [[UIApplication sharedApplication] statusBarFrame];
        __block CGRect vFrame = self.view.frame;
        __block CGFloat diff = sbFrame.size.height + sbFrame.origin.y - vFrame.origin.y;
        if (diff > 0.0)
        {
            [UIView animateWithDuration:UINavigationControllerHideShowBarDuration
                                  delay:0.0
                                options: UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 vFrame.origin.y += diff;
                                 vFrame.size.height -= diff;
                                 self.view.frame = vFrame;
                             }
                             completion:^(BOOL finished){
                                 NSLog(@"Done!");
                             }];
        }
    }
}