美文网首页
IOS 加载WebView进度条,及在WebView内返回上一

IOS 加载WebView进度条,及在WebView内返回上一

作者: Sulas | 来源:发表于2017-07-01 10:57 被阅读0次

1.定义属性

@property(nonatomic,strong)UIWebView *webView;

2.懒加载

/**********  webView  ************/

-(UIWebView *)webView{

if (_webView == nil) {

_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

_webView.delegate = self;

[self.view insertSubview:_webView atIndex:0];

}

return _webView;

}

3.在viewDidLoad中:

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];

4.webView内返回上一界面

/**********  返回按钮  ************/

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];

backBtn.frame = CGRectMake(self.view.width - 60, self.view.height - 40, 80, 30);

backBtn.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:.4];

backBtn.layer.masksToBounds = YES;

backBtn.layer.cornerRadius = 15;

[backBtn setTitle:@"返回" forState:UIControlStateNormal];

[backBtn addTarget:self action:@selector(GoBack:) forControlEvents:UIControlEventTouchUpInside];

[self.view insertSubview:backBtn atIndex:1];

5.返回按钮事件

#pragma mark - 返回按钮事件

-(void)GoBack:(UIButton *)sender{

//判断是否可以返回

if ([self.webView canGoBack]) {

//返回上一界面

[self.webView goBack];

}

}

6.自定义加载进度条

(一).自定义进度条类.h文件中

#import <UIKit/UIKit.h>

#import "UIView+Frame.h"

@interface WebviewProgressLine : UIView

//进度条颜色

@property (nonatomic,strong) UIColor  *lineColor;

//开始加载

-(void)startLoadingAnimation;

//结束加载

-(void)endLoadingAnimation;

@end

(二).自定义进度条类.m文件中

#import "WebviewProgressLine.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width

@implementation WebviewProgressLine

-(instancetype)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];

if (self) {

self.hidden = YES;

self.backgroundColor = [UIColor whiteColor];

}

return self;

}

-(void)setLineColor:(UIColor *)lineColor{

_lineColor = lineColor;

self.backgroundColor = lineColor;

}

-(void)startLoadingAnimation{

self.hidden = NO;

self.width = 0.0;

__weak UIView *weakSelf = self;

[UIView animateWithDuration:0.4 animations:^{

weakSelf.width = KScreenWidth * 0.6;

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.4 animations:^{

weakSelf.width = KScreenWidth * 0.8;

}];

}];

}

-(void)endLoadingAnimation{

__weak UIView *weakSelf = self;

[UIView animateWithDuration:0.2 animations:^{

weakSelf.width = KScreenWidth;

} completion:^(BOOL finished) {

weakSelf.hidden = YES;

}];

}

@end

7.OK这样,在Web界面使用就ok了

(一).在webView  Controller中定义进度条属性

@property (nonatomic,strong) WebviewProgressLine  *progressLine;///< 网页加载进度条

(二).懒加载进度条

/**********  进度条  ************/

-(WebviewProgressLine *)progressLine{

if (_progressLine == nil) {

_progressLine = [[WebviewProgressLine alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 4)];

_progressLine.lineColor = [UIColor greenColor];

}

return _progressLine;

}

(三).viewDidLoad中

[self.view addSubview:self.progressLine];

(四).webView代理

#pragma mark - UIWebViewDelegate

-(void)webViewDidStartLoad:(UIWebView *)webView{

[self.progressLine startLoadingAnimation];

}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

[self.progressLine endLoadingAnimation];

}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

[self.progressLine endLoadingAnimation];

}

相关文章

网友评论

      本文标题:IOS 加载WebView进度条,及在WebView内返回上一

      本文链接:https://www.haomeiwen.com/subject/axywcxtx.html