我最近升级到了xcode 5,但是当我在iOS模拟器中运行我的应用时,启动画面会重叠在状态栏上;而且当你进入应用时,状态栏也会重叠在我应用的元素上,比如我应用左上角的返回按钮。我的应用使用phonegap 2.9构建。有什么办法可以让它正确地渲染呢?
如果你使用的是Storyboard,你可以像这个问题iOS 7 - Status bar overlaps the view中那样解决此问题。
如果你没有使用Storyboard,则可以在AppDelegate.m
的didFinishLaunching
方法中使用此代码解决问题:
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);
}
同时请查看这个问题:iOS7中状态栏和导航栏的问题
在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];
}
height=device-height
,则可以切换到 width=device-width
,以在不受状态栏更改影响的情况下实现与以前相同的效果。 - Tsunamis我通常做的是向Info.plist
文件添加两个键值属性。
这些属性的源代码如下:
我在使用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];
}
这个解决方案并不是我自己的,但我找不到来源。希望它能帮到你!
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);
}
}
}
window.plugins.statusBar.hide();
window.plugins.statusBar.show();
window.plugins.statusBar.blackTint();
window.plugins.statusBar.whiteTint();
重要提示:在您的应用程序plist文件中,还需将UIViewControllerBasedStatusBarAppearance
设置为NO。
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
这适用于支持旧版iOS的情况。
实际上,我发现Gimi的回答是最好的答案,而且我已经找了很多!
为了补充Gimi的答案,并回答ignacio-munizaga对该答案的回复,代码将在视图出现后执行,这意味着在关闭内联浏览器或相机滚动等之后。因此,我只是放置了一个布尔值,说明大小是否已经被调整。
最终代码如下:
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];
}
尝试进入XCode中应用程序的(应用程序名称)-Info.plist
文件,并添加键。
view controller-based status bar appearance: NO
status bar is initially hidden : YES
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>