本文主要是介绍iOS之旅--封装NavigationController,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
KSNavigation
封装导航控制器
现在的应用一般都需要导航一致(返回按钮统一,标题背景统一等等)所以就根据UINavigationController封装了一个导航,具体核心如下:
* 统一设置返回按钮
* 统一设置UINavigationBar
* 由于封装会使右滑返回失效,需要添加代理设置
GitHub下载地址:KSNavigation—-github
如图:
以下为解决方案:
统一设置返回按钮
/*** 能拦截所有push进来的子控制器*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{if (self.viewControllers.count > 0) { // 如果现在push的不是栈底控制器(最先push进来的那个控制器)viewController.hidesBottomBarWhenPushed = YES;// 设置导航栏按钮UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];[button setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];[button setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];[button addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside];viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];}[super pushViewController:viewController animated:animated];
}
设置右滑返回代理
- 第一部分
@interface KSNavigationController ()<UIGestureRecognizerDelegate>@end
- 第二部分
- (void)viewDidLoad {…………//设置手势代理self.interactivePopGestureRecognizer.delegate = self;…………
}
- 第三部分
//手势代理
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{return self.childViewControllers.count > 1;
}
设置导航栏主题
//设置导航栏主题
- (void)setupNavigationBar
{UINavigationBar *appearance = [UINavigationBar appearance];//统一设置导航栏颜色,如果单个界面需要设置,可以在viewWillAppear里面设置,在viewWillDisappear设置回统一格式。[appearance setBarTintColor:[UIColor getColor:@"fb9c0a"]];//导航栏title格式NSMutableDictionary *textAttribute = [NSMutableDictionary dictionary];textAttribute[NSForegroundColorAttributeName] = [UIColor whiteColor];textAttribute[NSFontAttributeName] = [UIFont systemFontOfSize:15];[appearance setTitleTextAttributes:textAttribute];
}
手势右滑其实可以自己重新封装,可以实现整个屏幕右滑返回,上面的解决办法是官方的,只有左边部分区域支持右滑返回,
这篇关于iOS之旅--封装NavigationController的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!