iOS 返回按钮样式设置及相关问题

1.自定义返回按钮的同时, 保持iOS原生滑动返回手势(interactivePopGestureRecognizer)可用的方法
注意:
(1) 这样做会导致导航栏混乱, 即一个页面可能显示别的页面的导航栏, 甚至更严重的bug,效果如下:
(2) 如果只是单纯重设interactivePopGestureRecognizer的代理, 在push的过程中触发滑动返回会崩溃(好像还有别的问题), 所以需要在push动画过程中禁掉interactivePopGestureRecognizer手势

F5CAFB1B-672C-4258-BAF4-A3D19512F80A.png
1C9AC2E2-4C9D-4845-BF11-330FE8E3AB31.png
//
//  SENavigationViewController.m
//  SpeakEnglish
//
//  Created by Daniel on 16/4/1.
//  Copyright © 2016年 Daniel. All rights reserved.
//

#import "SENavigationViewController.h"
#import "UIImage+ZY.h"

@interface SENavigationViewController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@end

@implementation SENavigationViewController

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithRootViewController:rootViewController]) {
        [self setNavigationBarTheme];
        self.delegate = self;
    }
    return self;
}


// 主题
- (void)setNavigationBarTheme {
    
    // title属性
    UINavigationBar *bar = [UINavigationBar appearance];
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                           NSFontAttributeName:FontTitle};
    [bar setTitleTextAttributes:attr];
    
    // 背景颜色
    [bar setBackgroundImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)] forBarMetrics:UIBarMetricsDefault];
    [bar setShadowImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)]];
    
    
    // 按钮图片渲染
    [bar setTintColor:[UIColor whiteColor]];
    
    // 按钮属性
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    NSDictionary *itemAttr = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
    [item setTitleTextAttributes:itemAttr forState:UIControlStateNormal];
    [item setTintColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 自定义返回键(leftItem)后, 滑动返回不可用, 使用这种方式处理
    __weak typeof (self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 禁用返回手势, 防止崩溃
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    // 隐藏tabBar
    if (self.viewControllers.count == 1) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    // 定制返回键
    if (self.viewControllers.count >= 1) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
        
        [btn setImage:[UIImage templateImageWithName:@"nav_back"] forState:UIControlStateNormal];
        [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
        
        [btn addTarget:self action:@selector(popSelector) forControlEvents:UIControlEventTouchUpInside];
        btn.tintColor = [UIColor whiteColor];
        
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        viewController.navigationItem.leftBarButtonItem = item;
    }
    
    [super pushViewController:viewController animated:animated];
}

// 返回手势可用
- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)popSelector {
    [self popViewControllerAnimated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.visibleViewController.preferredStatusBarStyle;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

  1. '更换返回按钮箭头并去掉返回按钮文字'的方法, 这样不会出现奇怪的bug,
    缺点:
    (1). 是返回按钮样式自由度不够高
    (2). 隐藏导航栏后, 滑动返回不可用
//
//  SENavigationViewController.m
//  SpeakEnglish
//
//  Created by Daniel on 16/4/1.
//  Copyright © 2016年 Daniel. All rights reserved.
//

#import "SENavigationViewController.h"
#import "UIImage+ZY.h"

@interface SENavigationViewController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@end

@implementation SENavigationViewController

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithRootViewController:rootViewController]) {
        [self setNavigationBarTheme];
        self.delegate = self;
    }
    return self;
}


// 主题
- (void)setNavigationBarTheme {
    
    // title属性
    UINavigationBar *bar = [UINavigationBar appearance];
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                           NSFontAttributeName:FontTitle};
    [bar setTitleTextAttributes:attr];
    
    // 背景颜色
    [bar setBackgroundImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)] forBarMetrics:UIBarMetricsDefault];
    [bar setShadowImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)]];
    
    
    // 按钮图片渲染
    [bar setTintColor:[UIColor whiteColor]];
    // 设置返回按钮箭头
    [bar setBackIndicatorImage:[UIImage imageNamed:@"nav_back"]];
    [bar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back"]];
    
    // 按钮属性
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    NSDictionary *itemAttr = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
    [item setTitleTextAttributes:itemAttr forState:UIControlStateNormal];
    [item setTintColor:[UIColor whiteColor]];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 隐藏tabBar
    if (self.viewControllers.count == 1) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    // 去掉返回按钮文字
    UIBarButtonItem *backitem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    viewController.navigationItem.backBarButtonItem = backitem;
    
    [super pushViewController:viewController animated:animated];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.visibleViewController.preferredStatusBarStyle;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,945评论 4 60
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,056评论 25 707
  • 活到这个岁数,还没碰到过什么东西一眼看上就喜欢的不得了,彩铅是第一个。 这还得感谢我上二年级的宝贝女儿。陪女儿准备...
    云朵儿z阅读 596评论 3 9
  • 布达拉外的虔诚还是情殇 ※ 遥望,布达拉的顶, 那里,曾住进人,或神。 俯瞰,宫外山脚红墙外,...
    连捷一心阅读 578评论 6 4
  • 发现这里是写日记的好地方, 以后可以常来, 把心事儿说给自己听~ ·········
    7ac阅读 99评论 0 1