iOS 7 - 状态栏与视图重叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18775874/
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-08-31 01:09:41  来源:igfitidea点击:

iOS 7 - Status bar overlaps the view

iosstatusbarios7

提问by aryaxt

I have a ViewControllerwhich is inside a UINavigationcontroller, but the navigationBar is hidden. When I run the app on iOS 7, the status bar shows on top of my view. Is there a way to avoid this?

我有一个ViewController在 a 里面UINavigationcontroller,但是导航栏是隐藏的。当我在 iOS 7 上运行该应用程序时,状态栏显示在我的视图顶部。有没有办法避免这种情况?

I don't want to write any OS specific code.

我不想编写任何操作系统特定的代码。

Enter image description here

在此处输入图片说明

I tried setting View controller-based status bar appearanceto NO, but it did not fix the issue.

我尝试设置View controller-based status bar appearanceNO,但它没有解决问题。

采纳答案by aryaxt

Xcode5 has iOS 6/7 Deltaswhich is specifically made to resolve this issue. In the storyboard, I moved my views 20 pixels down to look right on iOS 7 and in order to make it iOS 6 compatible, I changed Delta yto -20.

Xcode5iOS 6/7 Deltas专门用于解决此问题。在故事板中,我将视图向下移动 20 像素以在 iOS 7 上看起来正确,为了使其与 iOS 6 兼容,我更改Delta y为 -20。

enter image description here

在此处输入图片说明

Since my storyboard is not using auto-layout, in order to resize the height of views properly on iOS 6 I had to set Delta heightas well as Delta Y.

由于我的故事板不使用自动布局,为了正确地调整对iOS 6的我的意见高度必须设置Delta height以及Delta Y

回答by Nathan H

If you simply do NOT want any status bar at all, you need to update your plist with this data: To do this, in the plist, add those 2 settings:

如果您根本不想要任何状态栏,则需要使用此数据更新您的 plist:为此,在 plist 中,添加这 2 个设置:

<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

In iOS 7 you are expected to design your app with an overlaid transparent status bar in mind. See the new iOS 7 Weather app for example.

在 iOS 7 中,您应该在设计应用程序时考虑到覆盖的透明状态栏。例如,请参阅新的 iOS 7 天气应用程序。

回答by Vincent

This is the default behaviour for UIViewControlleron iOS 7. The view will be full-screen which means the status bar will cover the top of your view.

这是UIViewControlleriOS 7 上的默认行为。视图将是全屏的,这意味着状态栏将覆盖视图的顶部。

If you have a UIViewControllerwithin a UINavigationControllerand the navigationBar is visible, you can have the following code in your viewDidLoador have a background image for navigationBar do the trick.

如果您在 aUIViewController内有 aUINavigationController并且导航栏可见,您可以在您的viewDidLoad或有导航栏的背景图像中使用以下代码来解决问题。

self.edgesForExtendedLayout = UIRectEdgeNone;

If you have navigationBar hidden, then you have to adjust all the UIView elements by shifting 20 points. I dont't see any other solution. Use auto layout will help a little bit.

如果您隐藏了导航栏,那么您必须通过移动 20 个点来调整所有 UIView 元素。我没有看到任何其他解决方案。使用自动布局会有所帮助。

Here is the sample code for detecting the iOS version, if you want to backward compatibility.

如果您想向后兼容,这里是检测 iOS 版本的示例代码。

NSUInteger DeviceSystemMajorVersion() {
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        NSString *systemVersion = [UIDevice currentDevice].systemVersion;
        _deviceSystemMajorVersion = [[systemVersion componentsSeparatedByString:@"."][0] intValue];
    });

   return _deviceSystemMajorVersion;
}

回答by comonitos

Only working solution i've made by my self.

我自己制作的唯一可行的解​​决方案。

Here is my UIViewController subclass https://github.com/comonitos/ios7_overlaping

这是我的 UIViewController 子类https://github.com/comonitos/ios7_overlaping

1 Subclass from UIViewController

1 UIViewController 的子类

2 Subclass your window.rootViewController from that class.

2 从该类中继承您的 window.rootViewController。

3 Voila!

3 瞧!

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

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect screen = [[UIScreen mainScreen] bounds];
        if (self.navigationController) {
            CGRect frame = self.navigationController.view.frame;
            frame.origin.y = 20;
            frame.size.height = screen.size.height - 20;
            self.navigationController.view.frame = frame;
        } else {
            if ([self respondsToSelector: @selector(containerView)]) {
                UIView *containerView = (UIView *)[self performSelector: @selector(containerView)];

                CGRect frame = containerView.frame;
                frame.origin.y = 20;
                frame.size.height = screen.size.height - 20;
                containerView.frame = frame;
            } else {
                CGRect frame = self.view.frame;
                frame.origin.y = 20;
                frame.size.height = screen.size.height - 20;
                self.view.frame = frame;
            }
        }
    }
}

4 Add this to make your status bar white Just right after the [self.window makeKeyAndVisible]; !!!

4 添加这个让你的状态栏变白 就在 [self.window makeKeyAndVisible] 之后;!!!

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

回答by Darshit Shah

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

-(void)viewWillLayoutSubviews{

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) 
  {
    self.view.clipsToBounds = YES;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenHeight = 0.0;
    if(UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
        screenHeight = screenRect.size.height;
    else
        screenHeight = screenRect.size.width;
    CGRect screenFrame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
    CGRect viewFrame1 = [self.view convertRect:self.view.frame toView:nil];
    if (!CGRectEqualToRect(screenFrame, viewFrame1))
    {
        self.view.frame = screenFrame;
        self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
  }
}

Add Key in plist--- View controller-based status bar appearance : NO

在 plist 中添加 Key --- 基于视图控制器的状态栏外观 : NO

回答by Mandeep Pasbola

To hide status bar in ios7 follow these simple steps :

要在 ios7 中隐藏状态栏,请按照以下简单步骤操作:

In Xcode goto "Resources" folder and open "(app name)-Info.plist file".

在 Xcode 中转到“ Resources”文件夹并打开“ (app name)-Info.plist file”。

  • check for "View controller based status bar appearance" key and set its value "NO"
  • check for "Status bar is initially hidden" key and set its value "YES"
  • 检查“ View controller based status bar appearance”键并设置其值“ NO
  • 检查“ Status bar is initially hidden”键并设置其值“ YES

If the keys are not there then you can add it by selecting "information property list" at top and click +icon

如果键不存在,那么您可以通过选择information property list顶部的“ ”并单击+图标来添加它

回答by Kalel Wade

If you wan to hide it completely and just avoid dealing with it, this works well.

如果您想完全隐藏它并避免处理它,这很有效。

-(BOOL) prefersStatusBarHidden
{
    return YES;
}

reference https://stackoverflow.com/a/18873777/1030449

参考https://stackoverflow.com/a/18873777/1030449

回答by tipycalFlow

If using xibs, a very easy implementation is to encapsulate all subviews inside a container view with resizing flags (which you'll already be using for 3.5" and 4" compatibility)so that the view hierarchy looks something like this

如果使用xibs,一个非常简单的实现是将所有子视图封装在具有调整大小标志的容器视图中(您已经使用它来实现 3.5" 和 4" 兼容性),以便视图层次结构看起来像这样

xib heirarchy

xib 等级制度

and then in viewDidLoad, do something like this:

然后在viewDidLoad,做这样的事情:

- (void)viewDidLoad
{
  [super viewDidLoad];
  // initializations
  if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) // only for iOS 7 and above
  {
    CGRect frame = containerView.frame;
    frame.origin.y += 20;
    frame.size.height -= 20;
    containerView.frame = frame;
  }
}

This way, the nibs need not be modified for iOS 7 compatibility. If you have a background, it can be kept outside containerViewand let it cover the whole screen.

这样,无需修改笔尖以兼容 iOS 7。如果你有背景,它可以放在外面containerView,让它覆盖整个屏幕。

回答by Tony

This is all that is needed to remove the status bar. enter image description here

这就是删除状态栏所需的全部内容。 在此处输入图片说明

回答by Saru

For Navigation Bar :

对于导航栏:

Writing this code :

编写此代码:

self.navigationController.navigationBar.translucent = NO;

just did the trick for me.

刚刚为我做了伎俩。