方法一:
先在Info.plist中添加:View controller-based status bar appearance 并将其值设置为NO。
在MainViewController.m实现类文件中修改viewWillAppear方法,修改内容如下:
作用是更改View的边界,使其下移20px,刚好是状态栏的高度。
- (void)viewWillAppear:(BOOL)animated
{
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];
}
如果程序涉及到打开系统相册或照相机功能时,需要在该类@implementation下声明一个变量,用来只让设置高度-20点代码运行一次,并修改刚才添加点if判断中的代码块,如:
int count = 1;
- (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.
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
if(count != nil && count == 1){
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height-20;
self.webView.frame = viewBounds;
count -= 1;
}
}
[super viewWillAppear:animated];
}
方法二:
给index.html的body设置css样式:
padding-bottom:20px
而且这样还有一个好处就是状态栏可以透明。
推荐使用方法一。