在开发中,我们经常会用到UIWebView控件,特别是HTML5发布之后,我们可以利用WebView嵌入H5。现在就来简单的使用下WebView。
UIWebView 继承与UIView并且遵守UIScrollViewDelegate协议。
1.设置Info.plist文件的字段
设置好字段之后,ViewController.m中的代码
//
// ViewController.m
// 乐回收
//
// Created by 赵前 on 16/6/2.
// Copyright © 2016年 赵前. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
UIWebView *_webView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
NSURL *url = [NSURL URLWithString:@"http://www.lehuiso.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[_webView loadRequest:req];
_webView.scalesPageToFit = YES;
_webView.backgroundColor = [UIColor whiteColor];
_webView.scrollView.showsVerticalScrollIndicator = FALSE;
[self.view addSubview:_webView];
}
//下面的这部分代码是为了将WebView和状态栏分开,iOS7之前,状态栏和webView是分开的,之后是WebView是占满整个屏幕,所以为了将其分开,写了下面的代码
-(void)viewWillAppear:(BOOL)animated{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = [_webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
_webView.frame = viewBounds;
}
[super viewWillAppear:animated];
}
@end