在iOS 7中,Phonegap应用程序将出现在状态栏下面。这可能会使位于屏幕顶部的按钮/菜单难以点击。
是否有人知道如何在Phonegap应用程序中修复iOS 7上的状态栏问题?
我已经尝试使用CSS偏移整个网页,但似乎不起作用。是否有一种方法可以像在iOS 6中一样改变整个UIWebView的偏移量或只是让状态栏的行为像以前一样?
谢谢
在iOS 7中,Phonegap应用程序将出现在状态栏下面。这可能会使位于屏幕顶部的按钮/菜单难以点击。
是否有人知道如何在Phonegap应用程序中修复iOS 7上的状态栏问题?
我已经尝试使用CSS偏移整个网页,但似乎不起作用。是否有一种方法可以像在iOS 6中一样改变整个UIWebView的偏移量或只是让状态栏的行为像以前一样?
谢谢
我在另一个帖子上找到了答案,但为了避免其他人产生疑问,在这里回答一下这个问题。
只需将MainViewController.m
中的viewWillAppear
替换为以下内容:
- (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 = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
}
[super viewWillAppear:animated];
}
除了Ludwig Kristoffersson的调整大小修复外,我建议更改状态栏颜色:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
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;
}
self.view.backgroundColor = [UIColor blackColor];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
if(screenSize == 0)
{
viewBounds.size.height = viewBounds.size.height - 20;
screenSize = viewBounds.size.height;
}
self.webView.frame = viewBounds;
}
我这样做是因为每次进入全屏模式时,我的应用程序都会调整大小。 - magorich请安装该插件以便在PhoneGap中使用:https://build.phonegap.com/plugins/505
并使用以下正确设置来控制Webview的覆盖层:
<preference name="StatusBarOverlaysWebView" value="false" />
对我来说,使用Phonegap 3.3.0是可行的。
更多信息,请查看Github项目页面: https://github.com/phonegap-build/StatusBarPlugin
MainViewController.m
的-(void)viewDidUnload
函数下,添加以下代码以隐藏状态栏。- (BOOL)prefersStatusBarHidden
{
return YES;
}
这个答案对我很有用,但是我需要稍微修改一下才能让它与相机插件(和其他插件)以及一个带有“height = device-height”的viewport meta标签一起使用(如果不设置高度部分,则会导致键盘在我的情况下出现在视图上,沿途隐藏一些输入)。
每次打开相机视图并返回到您的应用程序时,viewWillAppear方法将被调用,并且您的视图将缩小20px。
此外,viewport的device-height将包括额外的20px,使内容可滚动,并比webview高20px。
以下是相机问题的完整解决方案:
In MainViewController.h:
@interface MainViewController : CDVViewController
@property (atomic) BOOL viewSizeChanged;
@end
@implementation MainViewController
@synthesize viewSizeChanged;
[...]
- (id)init
{
self = [super init];
if (self) {
// On init, size has not yet been changed
self.viewSizeChanged = NO;
// Uncomment to override the CDVCommandDelegateImpl used
// _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
// Uncomment to override the CDVCommandQueue used
// _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
}
return self;
}
[...]
- (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 not already done
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && !self.viewSizeChanged) {
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
self.viewSizeChanged = YES;
}
[super viewWillAppear:animated];
}
if (window.device && parseFloat(window.device.version) >= 7) {
$(window).on('orientationchange', function () {
var orientation = parseInt(window.orientation, 10);
// We now the width of the device is 320px for all iphones
// Default height for landscape (remove the 20px statusbar)
var height = 300;
// Default width for portrait
var width = 320;
if (orientation !== -90 && orientation !== 90 ) {
// Portrait height is that of the document minus the 20px of
// the statusbar
height = document.documentElement.clientHeight - 20;
} else {
// This one I found experimenting. It seems the clientHeight
// property is wrongly set (or I misunderstood how it was
// supposed to work).
// I don't know if it is specific to my setup.
width = document.documentElement.clientHeight + 20;
}
document.querySelector('meta[name=viewport]')
.setAttribute('content',
'width=' + width + ',' +
'height=' + height + ',' +
'initial-scale=1.0,maximum-scale=1.0,user-scalable=no');
})
.trigger('orientationchange');
}
以下是我用于其他版本的视口:
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0" />
对于Cordova 3.1+,有一个插件可以处理iOS 7+状态栏行为的更改。
这个文档很好地记录在这里,包括如何将状态栏恢复到其iOS7之前的状态。
要安装插件,请运行:
cordova plugin add org.apache.cordova.statusbar
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarStyle" value="default" />
参见:http://plugins.cordova.io/#/package/org.apache.cordova.statusbar - ezzadeen尝试进入XCode中应用程序的(应用程序名称)-Info.plist
文件,并添加以下键:
view controller-based status bar appearance: NO
status bar is initially hidden : YES
我通过安装org.apache.cordova.statusbar
和在我的顶部的config.xml
中添加以下内容来解决了我的问题:
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarBackgroundColor" value="#000000" />
<preference name="StatusBarStyle" value="lightcontent" />
这些配置仅适用于iOS 7及以上版本。
另一种方法是实现向后兼容。根据的HTML(顶部额外20像素的边距)来展示全屏
效果,并在iOS < 7.0
中去除这个额外的边距。类似于以下在MainViewController.m
中的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = -20;
viewBounds.size.height = viewBounds.size.height + 20;
self.webView.frame = viewBounds;
}
}