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

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开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,733评论 2 7
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,742评论 0 3
  • VLC的集成和使用 VLC介绍 VLC Media Player (VideoLAN) 为 Windows、Lin...
    Pocket阅读 19,966评论 75 66
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,556评论 1 14
  • 我的目标:3个月内工作能力全面提升,拥有高圆圆般灿烂的微笑。 动机:踏实干事,不忘记自己的使命。 拔草:1.做分享...
    超人菲菲目标必达阅读 220评论 0 1