iOS之旅--封装NavigationController
2015-12-09 13:49
573 查看
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控件循环旋转
- iOS Tips
- 2015-12-IOS 获取最新设备型号方法
- IOS_多线程编程4 - GCD
- iOS开发-工具:Xcode7升级之后插件无法使用与不小心点击Skipbundle的解决办法
- iOS 中二维码扫描
- ios加密后的数据保存本地,解密时为何失败
- iOS Autolayout基础-Masonry
- IOS_多线程编程2 - NSOperation
- ios9 的新特性
- IOS 多线程编程1 - NSThread
- iOS CoreData 存取数组字典等数据类型
- 分享一下我封装iOS自定义控件的体会,附上三个好用的控件Demo <时间选择器&多行输入框&日期选择器>
- iOS立方体翻转3D特效
- iOS 在Xcode中添加多个模拟器
- ios同一个Label中的字符设置不同的字体
- iOS 保持界面流畅的技巧(转)
- iOS-布局-Masonry
- 导航栏透明 毛玻璃效果iOS
- ios正在使用NSDateComponents、NSDate、NSCalendar它的结论是在当前时间是在一段时间在一天。