【问题标题】:iOS 7 status bar overlapping UIiOS 7 状态栏重叠 UI
【发布时间】:2013-09-24 00:53:22
【问题描述】:

我最近升级到 xcode 5,当我在 iOS 模拟器中运行我的应用程序时,初始屏幕会与状态栏重叠,当您在应用程序中时,状态栏会与我的应用程序上的元素重叠,例如我的后退按钮我的应用程序的左上角。我使用 phonegap 2.9 构建我的应用程序。有什么想法可以让我正确渲染。

【问题讨论】:

标签: iphone cordova ios7 xcode5


【解决方案1】:

如果您使用故事板,则可以解决此问题,如以下问题:iOS 7 - Status bar overlaps the view

如果您不使用情节提要,那么您可以在 AppDelegate.m 中的 did finishlaunching 中使用此代码:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.clipsToBounds =YES;
    self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}

另见这个问题:Status bar and navigation bar issue in IOS7

【讨论】:

  • 谢谢你的工作。它应该只显示一个黑条还是可以像在 iOS6 中一样显示时间、电池图标?
  • 嘿,我的状态栏部分不可见。它的 Balck 使用此代码。你能帮我为什么吗?
  • 通过这个技巧,状态栏不再可见,但这根本不是问题。效果很好,谢谢!
  • 你也可以使用这个链接stackoverflow.com/questions/18980925/…
  • self.window.frame.size 对我来说是空的,所以我不得不使用:CGRect screenBounds = [[UIScreen mainScreen] bounds]; screenBounds.size.width;此外,对于横向,最后一行看起来像这样: self.window.frame = CGRectMake(20,0,screenBounds.size.width-20,screenBounds.size.height);
【解决方案2】:

MainViewController.m里面:- (void)viewWillAppear:(BOOL)animated添加这个:

//Lower screen 20px on ios 7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 18;
        viewBounds.size.height = viewBounds.size.height - 18;
        self.webView.frame = viewBounds;
    }

所以结束函数将如下所示:


- (void)viewWillAppear:(BOOL)animated
{
    // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
    // you can do so here.
    //Lower screen 20px on ios 7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 18;
        viewBounds.size.height = viewBounds.size.height - 18;
        self.webView.frame = viewBounds;
    }
    [super viewWillAppear:animated];
}

【讨论】:

  • 这是 phonegap/cordova 应用程序的最佳解决方案。轻微的挑剔 - 你应该使用 20 而不是 18 吗?
  • inAppBrowser 有问题。关闭使用 inAppBrowser 打开的网页时,您会在底部看到一个 18 像素的白条。 ¿ 有什么解决办法吗?
  • 我遇到了与 Ignacio 相同的问题,在显示另一个本机视图(例如相机捕获对话框)然后导航回 webView 后,我的应用程序底部显示了一个白条。每次显示/关闭第二个本机对话框时,白条都会增长。修复方法是将“viewBounds”初始化为“[self.view bounds]”而不是“[self.webView bounds]”。
  • 我爱你 - 浪费了 3 天的时间来解决这个问题。其他解决方案在设备旋转方面存在问题,并且会剪辑我的应用程序的内容。这是最好的 PhoneGap 解决方案!
  • @JanDudek 如果您使用的是height=device-height,您可以切换到width=device-width 与之前的效果相同,而不受状态栏变化的影响。
【解决方案3】:

我通常做的是将两个键值属性添加到Info.plist 文件中。

属性源代码为:

【讨论】:

  • 这绝对是最容易实现并且对我有用的答案。
  • 这个问题,就科尔多瓦植入而言,当您以横向启动应用程序时,它会以纵向呈现应用程序。然后,你有一个占据屏幕 1/4 的条形图。还没有找到解决办法
  • 最佳答案,但是如何在构建时通过顶级 config.xml 添加它?
【解决方案4】:

我在使用 phonegap 打开 inApp 浏览器时遇到问题。 Gimi 的修复效果很好,但每次我打开 inApp 浏览器时,屏幕底部都会缩小。所以我添加了一个 if 语句来检查 webview 的 y 原点是否为 0。inApp 浏览器不再有 y 原点 0,所以它解决了我的问题。

// ios 7 status bar fix
- (void)viewWillAppear:(BOOL)animated
{
    // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
    // you can do so here.
    //Lower screen 20px on ios 7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        if(self.webView.frame.origin.y == 0) {
            CGRect viewBounds = [self.webView bounds];
            viewBounds.origin.y = 20;
            viewBounds.size.height = viewBounds.size.height - 20;
            self.webView.frame = viewBounds;
        }
    }

    [super viewWillAppear:animated];
}

解决方案不是真正的我,但我找不到源。希望对您有所帮助!

【讨论】:

  • 您的 hack 可以节省一天的时间。我的客户一直在我老板的办公室里举起危险信号。谢谢先生。
【解决方案5】:

将此代码 sn-p 放在 phonegap 项目的 MainViewController.m 文件中。

- (void)viewDidLayoutSubviews{

    if ([self respondsToSelector:@selector(topLayoutGuide)]) // iOS 7 or above
    {
        CGFloat top = self.topLayoutGuide.length;
        if(self.webView.frame.origin.y == 0){
            // We only want to do this once, or 
            // if the view has somehow been "restored" by other    code.
            self.webView.frame = CGRectMake(self.webView.frame.origin.x,
                                           self.webView.frame.origin.y + top,
                                           self.webView.frame.size.width,
                                           self.webView.frame.size.height-top);
        } 
    } 
}

【讨论】:

    【解决方案6】:

    我在 ViewDidLoad 方法中使用此代码。

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

    这在尝试支持以前的 iOS 版本时也有效。

    【讨论】:

      【解决方案7】:

      实际上,我发现 Gimi 的答案是最好的答案,而且我一直在寻找很多! 只是为了添加 Gimi 的答案,并回答 ignacio-munizaga 对该答案的回复,代码将在视图出现时执行,这意味着在您关闭内联浏览器或相机胶卷等之后,所以我只是放了一个 bool 值说明是否已经调整大小。

      最终代码如下:

      bool sizeWasAdjusted = false;
      - (void)viewWillAppear:(BOOL)animated
      {
          // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
          // you can do so here.
          //Lower screen 20px on ios 7
          if (!sizeWasAdjusted && [[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
              CGRect viewBounds = [self.webView bounds];
              viewBounds.origin.y = 18;
              viewBounds.size.height = viewBounds.size.height - 18;
              self.webView.frame = viewBounds;
              sizeWasAdjusted = true;
          }
          [super viewWillAppear:animated];
      }
      

      【讨论】:

        【解决方案8】:

        尝试在 XCode 中进入应用程序的 (app name)-Info.plist 文件并添加密钥

        view controller-based status bar appearance: NO
        status bar is initially hidden : YES
        

        这对我来说似乎没有问题。

        【讨论】:

          【解决方案9】:

          如果您为 iOS 7 开发,我不建议将状态栏包装成黑色矩形旧 iOS 样式。只需将其集成到设计中即可获得更多 iOS 7“全屏”外观。

          您可以使用此插件来调整状态栏的墨水颜色,或者在您的应用的任何实例中隐藏或显示它。

          https://github.com/jota-v/cordova-ios-statusbar

          js方法有:

          window.plugins.statusBar.hide();
          window.plugins.statusBar.show();
          window.plugins.statusBar.blackTint();
          window.plugins.statusBar.whiteTint();
          

          重要提示:同样在您的应用 plist 文件中将 UIViewControllerBasedStatusBarAppearance 设置为 NO

          【讨论】:

            【解决方案10】:

            最好把这些东西放在 info.plist 中

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

            这根本没有状态栏。

            【讨论】:

              【解决方案11】:

              为了解决iOS7的状态栏问题,我们可以使用如下代码来Cordova/PhoneGap:

              function onDeviceReady() {
                  if (parseFloat(window.device.version) === 7.0) {
                        document.body.style.marginTop = "20px";
                  }
              }
              
              document.addEventListener('deviceready', onDeviceReady, false);
              

              无论如何,Cordova 3.1 很快就会解决 iOS7 的这个问题和其他问题。

              【讨论】:

              • 看起来它在 3.1 中没有得到修复
              • 确实,cordova 3.1 没有解决这个问题。我正在使用cordova 3.1,作为解决方法,我消失了状态栏(修改plist),因为几种偏移方法对我不起作用,而从我的角度来看,其他一些方法很混乱。至少对我来说,状态栏对于我的应用来说不是必须的。
              • 在 3.3 中也没有修复。 :(
              • 从外观上看,在 3.5 中仍未修复。
              【解决方案12】:
               if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
              
                      self.window.clipsToBounds =YES;
              [application setStatusBarStyle:UIStatusBarStyleLightContent];
               self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
                  }
              

              【讨论】:

                【解决方案13】:

                来自Apple iOS7 transition Guide

                具体来说,

                self.automaticallyAdjustsScrollViewInsets = YES;
                self.edgesForExtendedLayout = UIRectEdgeNone;
                

                当我不想重叠并且我有一个 UITableViewController 时适合我。

                【讨论】:

                  【解决方案14】:

                  如果您将 Cordova 与 Sencha Touch 一起使用,并且您使用的是普通 Sencha Touch 导航/标题栏,您只需拉伸导航栏并重新定位导航栏中的内容,使其在 iOS 7 上看起来像家一样。只需添加这进入app.js(在您最后的Ext.Viewport.add 行之后):

                  // Adjust toolbar height when running in iOS to fit with new iOS 7 style
                  if (Ext.os.is.iOS && Ext.os.version.major >= 7) {
                    Ext.select(".x-toolbar").applyStyles("height: 62px; padding-top: 15px;");
                  }
                  

                  (来源:http://lostentropy.com/2013/09/22/ios-7-status-bar-fix-for-sencha-touch-apps/

                  这有点老套,我知道,但它省去了在 Objective-C 级别处理它的麻烦。不过,对于那些不使用 Sencha Touch 和 Cordova 的人来说,这并不是什么好事。

                  【讨论】:

                    【解决方案15】:

                    didFinishLaunchingWithOptions 事件中的 AppDelegate.m 中编写以下代码(正好在其最后一行代码“return YES;”之前):

                    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) 
                    {
                        [application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
                    }
                    

                    我会等待您的反馈! :)

                    【讨论】:

                    • 如果您要复制此解决方案并粘贴到您的教程中,请参考!谢谢
                    • 这对我不起作用(即它没有隐藏状态栏)。然而,这个涉及在MainViewController.m 中设置首选项的答案似乎可以正常工作:stackoverflow.com/a/19571259/539883
                    【解决方案16】:

                    在.plist文件中设置属性:

                    <key>UIViewControllerBasedStatusBarAppearance</key>
                    <false/>
                    

                    它隐藏了状态栏。

                    【讨论】:

                      【解决方案17】:

                      您可以使用的最佳方法是调整主视图的大小,特别是在您的应用程序使用页脚时。

                      在 MainViewController.m 上使用 viewDidLoad 方法,在 [super viewDidLoad]; 之后

                      NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
                      if ([[vComp objectAtIndex:0] intValue] >= 7) {
                          // iOS 7 or above
                          CGRect oldBounds = [self.view bounds];
                          CGRect newViewBounds = CGRectMake( 0, -10, oldBounds.size.width, oldBounds.size.height-20 );
                          CGRect newWebViewBounds = CGRectMake( 0, -20, oldBounds.size.width, oldBounds.size.height-40 );
                      
                          [self.view setBounds:newViewBounds];
                          [self.webView setBounds:newWebViewBounds];
                      }
                      

                      那么您就不需要在您的应用程序中修改任何 javascript

                      【讨论】:

                        【解决方案18】:

                        随着 iOS 7.0.4 的发布,iOS7 修复在我当前的项目中中断,因为 7.0.4 没有修复。 因此,如果运行次要版本 3 或更低版本,则添加一个次要控件以运行它。

                        NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
                        if ([[vComp objectAtIndex:0] intValue] == 7 && [[vComp objectAtIndex:1] intValue] == 0 && [[vComp      objectAtIndex:2] intValue] <= 3 ) {
                            CGRect oldBounds = [self.view bounds];
                            CGRect newViewBounds = CGRectMake( 0, -10, oldBounds.size.width, oldBounds.size.height-20 );
                            CGRect newWebViewBounds = CGRectMake( 0, -20, oldBounds.size.width, oldBounds.size.height-40 );
                            [self.view setBounds:newViewBounds];
                            [self.webView setBounds:newWebViewBounds];
                        }
                        

                        【讨论】:

                          【解决方案19】:

                          我开始走这条路,但我有自己的导航控件,带有core view,我将其滑入通常用通用标题栏和tabbar 包裹核心的框架中;没有storyboard,没有UINavigationController

                          它开始快速变得复杂,添加和减去状态栏的 20 个像素。然后顿悟了。

                          我复制了.xib file,告诉Xcode 将其作为iOS 7 布局展示给我,重新对齐所有内容,并在代码中放置一个6/7 开关.. 立即,一切正常,并且附加资源仅将 8K 添加到捆绑包中。

                          【讨论】:

                            【解决方案20】:

                            我首先为 iOS7 开发,但为了保持与 iOS6 的兼容性,我与 Gimi 的建议完全相反 - 在 MainViewController.m 我将 y 原点设置为 20px 并将视图高度增加 20px:

                            //Lower screen 20px on ios 7
                            
                            if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
                              CGRect viewBounds = [self.webView bounds];
                                viewBounds.origin.y = -20;
                                viewBounds.size.height = viewBounds.size.height + 20;
                                self.webView.frame = viewBounds;
                            }
                            

                            【讨论】:

                              【解决方案21】:

                              我们也可以在我们的 js 文件中检查这一点

                              if (parseFloat(window.device.version) >= 7.0) {
                              
                                  $("body").addClass("ios7");
                              }
                              

                              在 .css 文件中为此定义一个类

                               .ios7 .ui-page, .ios7 .ui-header, .ios7 .ui-pane {
                                   margin-top: 17px !important;
                                }
                              

                              【讨论】:

                                【解决方案22】:

                                如果您将屏幕绑定设置为 20px,则上述某些答案会在顶部显示一个黑条,如果您使用 webview.frame.size.height -20px,其他答案将继续缩小您的 webview 屏幕; 那就试试这个吧,任何ios都可以,不用改css,放在里面

                                - (void)webViewDidFinishLoad:(UIWebView*)theWebView {
                                

                                //是IOS7以上吗

                                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
                                

                                //获取设备屏幕尺寸

                                    CGRect screenBounds = [[UIScreen mainScreen] bounds];
                                

                                //将高度降低20px

                                    int x= screenBounds.size.height -20;
                                

                                //设置webview top pos 20px,使其位于状态栏下方,缩小20px

                                    [theWebView setFrame:CGRectMake(0, 20, theWebView.frame.size.width, x )];
                                
                                }
                                

                                【讨论】:

                                  【解决方案23】:

                                  适用于 iPhoneX

                                  以上所有答案:

                                  iPhoneX中状态栏的高度是44而不是20

                                  【讨论】:

                                    【解决方案24】:

                                    这是我使用 CSS 和 Javascript 的方法:

                                    1) 在您的 CSS 中定义以下内容:

                                    #ios7-statusbar-fix {
                                        width:100%;
                                        height:20px;
                                        background-color:white;
                                        position:fixed;
                                        z-index:10000;
                                        margin-top:-20px;
                                        display:none;
                                    }
                                    

                                    2)&lt;body&gt;-tag 之后添加具有此ID 的div-container 作为第一个元素:

                                    <body>
                                        <div id="ios7-statusbar-fix"></div>
                                    …
                                    

                                    3) 过滤 iOS 7 设备并通过 Javascript 应用更改:

                                    if (navigator.userAgent.match(/(iPad.*|iPhone.*|iPod.*);.*CPU.*OS 7_\d/i)) {
                                            document.body.style.marginTop = '20px';
                                            document.getElementById('ios7-statusbar-fix').style.display = 'block';
                                    }
                                    

                                    【讨论】:

                                      【解决方案25】:

                                      非常感谢伙计。它在以下方面运行良好:

                                      MAC OS X 10.9.1 - Xcode 5.0.2 - cordova 3.3 - jQuery mobile 1.3.2

                                      【讨论】:

                                        猜你喜欢
                                        • 1970-01-01
                                        • 2015-01-21
                                        • 1970-01-01
                                        • 2014-05-10
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        相关资源
                                        最近更新 更多