屏幕快照 2017-09-29 下午2.53.59.png
屏幕快照 2017-09-29 下午2.53.38.png
- (void)viewDidLoad {
[self addLeftButton];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//判断是否有上一层H5页面
if ([webView canGoBack]) {
//如果有则返回
//同时设置返回按钮和关闭按钮为导航栏左边的按钮
self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];
} else {
self.navigationItem.leftBarButtonItems = @[self.backItem];
}
}
//点击返回的方法
- (void)backNative
{
//判断是否有上一层H5页面
if ([webView canGoBack]) {
//如果有则返回
[webView goBack];
} else {
[self closeNative];
}
}
//关闭H5页面,直接回到原生页面
- (void)closeNative
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - init
- (UIBarButtonItem *)backItem
{
if (!_backItem) {
_backItem = [[UIBarButtonItem alloc] init];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"返回"];
[btn setImage:image forState:UIControlStateNormal];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(backNative) forControlEvents:UIControlEventTouchUpInside];
[btn.titleLabel setFont:[UIFont systemFontOfSize:17]];
// [btn setTitleColor:[UIColor sy_backColor] forState:UIControlStateNormal];
//字体的多少为btn的大小
[btn sizeToFit];
//左对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//让返回按钮内容继续向左边偏移15,如果不设置的话,就会发现返回按钮离屏幕的左边的距离有点儿大,不美观
btn.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
btn.frame = CGRectMake(0, 0, 40, 40);
_backItem.customView = btn;
}
return _backItem;
}
- (UIBarButtonItem *)closeItem
{
if (!_closeItem) {
_closeItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeNative)];
}
return _closeItem;
}
网友评论