iOS 关于webView的使用方法

时间:2023-03-09 20:20:47
iOS 关于webView的使用方法

关于webView的使用方法还是比较简单的。
直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@interface ViewController () <UIWebViewDelegate>//使用代理
{
    UIWebView *webView;
    UIView *_coverView;
    UIActivityIndicatorView *_activityIndicator;//风火轮
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
     
    //获取当前屏幕的宽高
    int ScreenHeight = [[UIScreen mainScreen] bounds].size.height;
    int ScreenWidth = [[UIScreen mainScreen] bounds].size.width;
     
    //获取状态栏的高度
    CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
    int statusBarHeight = statusBarRect.size.height;
     
    //设置状态栏的尺寸,位置
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, statusBarHeight, ScreenWidth, ScreenHeight - statusBarHeight)];
    //设置请求的URL
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    [self.view addSubview:webView];
    //设置webView的代理
    webView.delegate = self;
    //加载这个URL
    [webView loadRequest:request];
     
    //smallBgView
    _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight, ScreenWidth, ScreenHeight - statusBarHeight)];
    _coverView.backgroundColor = [UIColor blackColor];
    _coverView.alpha = 0.5;
     
    //Activity Indicator
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((ScreenWidth - 30)/2 , (ScreenHeight - statusBarHeight - 30)/2, 30, 30)];
    _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    _activityIndicator.hidesWhenStopped = YES;
    _activityIndicator.color = [UIColor whiteColor];
    _activityIndicator.alpha = 1.0;
    [_coverView addSubview:_activityIndicator];
     
}
//在webView开始加载时会调用该函数,我们在这里显示coverView
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.view addSubview:_coverView];
    [_activityIndicator startAnimating];
}
//在webView加载完毕时会调用该函数,我们在这里把coverView移除掉
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_coverView removeFromSuperview];
    [_activityIndicator stopAnimating];
}

看下演示效果
iOS 关于webView的使用方法

放个demo,供大家联系
WebViewTest

本文永久地址:http://blog.it985.com/7529.html
本文出自 IT985博客 ,转载时请注明出处及相应链接。