iOS7以上自定义一个navigationController,并沿用系统的侧滑返回效果

首先需要子类化一个navigationController的子类,在init方法中对定制nav的一些基本需求进行设置

 1 - (instancetype)initWithRootViewController:(UIViewController *)rootViewController
 2 {
 3     if (self = [super initWithRootViewController:rootViewController]) {
 4         // 设置navigationBar的背景颜色,根据需要自己设置
 5         self.navigationBar.barTintColor = redButtonColor;
 6         // 设置navigationBar是否透明,不透明的话会使可用界面原点下移(0,0)点为导航栏左下角下方的那个点
 7         self.navigationBar.translucent = NO;
 8         // 设置navigationBar是不是使用系统默认返回,默认为YES
 9         self.interactivePopGestureRecognizer.enabled = YES;
10         // 创建一个颜色,便于之后设置颜色使用
11         UIColor * color = [UIColor whiteColor];
12         // 设置navigationBar元素的背景颜色,不包括title
13         self.navigationBar.tintColor = color;
14         // 设置navigationController的title的字体颜色
15         NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
16         self.navigationBar.titleTextAttributes = dict;
17     }
18     
19     return self;
20 }

因为这个导航栏是你自己自定义的,所以默认的侧滑返回会失效,接下来要在viewDidLoad中从新遵循手势的代理

- (void)viewDidLoad
{
    // 为self创建弱引用对象
    __weak typeof (self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}

OK,完成

原文地址:https://www.cnblogs.com/LureBlog/p/4331907.html