1、绑定一个转跳按钮触发函数
-(void)login:(UIButton *)btn
{
LoginViewController *lVC = [[LoginViewController alloc]init];//目标页面
[self.navigationController pushViewController:lVC animated:YES];
}
2、设置WebView
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
//返回按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(16, 30, 20, 20)];
[btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//返回按钮触发事件
-(void)back:(UIButton *)btn
{
[self.navigationController popViewControllerAnimated:YES];
}
//设置webView
-(void) initUI
{
WKWebView *WebView = [[WKWebView alloc] init];
WebView.UIDelegate = self;
WebView.navigationDelegate = self;//设置两个代理
[self.view addSubview:WebView];
//约束
[WebView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).with.offset(0);
make.top.equalTo(self.view).with.offset(58);
make.right.equalTo(self.view).with.offset(0);
make.bottom.equalTo(self.view).with.offset(88);
}];
//设置转跳到的网址
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[WebView loadRequest:request];
}
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"开始加载网页");
}
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"加载完成");
}
//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"加载失败");
}
@end