您的位置:首页 > 其它

自定义NavigationCotroller

2016-07-10 13:39 225 查看
[iOS]自定义NavigationController的一般过程

在项目中,我们经常会使用UINavigationController来管理一组控制器,但是,如果我们使用系统自带的NavigationController,可能会造成许多意想不到的问题,比如说返回手势的失效,NavigationBar颜色设置的不一致(由于透明度造成),或者是当NavigationController嵌套在UITabbarController中使用时,在push过程中,tabor何时消失的不确定等等问题,所以我们经常使用自定义的NavigationController来控制一组控制器,过程如下:
1.继承UINavigationController来实现自己的NavigationController。
2.解决返回手势失败的问题。
3.解决NavigationBar颜色设置不一致的问题。
4.解决push时隐藏Tabbar。
5.设置整个NavigationController状态栏的样式,注意:在iOS7之后,修改状态栏样式的方法不被提供了,而是改为了控制器自己重写方法
- (UIStatusBarStyle

[/code]

)preferredStatusBarStyle;来实现。但是如果控制器被NavigationController所管理,那么该方法只会调用一次,即调用栈底层的控制器的该方法,其他控制器的该方法会被截断。

首先先说第一点,这一个很简单,直接创建一个继承自UINavigationController的控制器即可,例如我的MDMNavigationController。代码如下:.h文件

</pre><p style="margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', Simsun; font-size: 17px;"></p><p style="margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', Simsun; font-size: 17px;">第二点:解决返回手势失效的问题我们可以定义一个属性来保存NavigationController的interactivePopGestureRecognizer的delegate来解决该问题,代码如下:</p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-size: 17px;"><code></code>
@interface
MDMNavigationController ()<<span se-mark="1">UINavigationControllerDelegate>@property (nonatomic, weak) idPopDelegate;@end@implementation MDMNavigationController- (void)viewDidLoad { [super viewDidLoad]; self.PopDelegate =self.interactivePopGestureRecognizer.delegate; self.delegate = self; } - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController == self.viewControllers[0]) {self.interactivePopGestureRecognizer.delegate = self.PopDelegate; }else{self.interactivePopGestureRecognizer.delegate = nil; } }@end


第三点:解决NavigationBar颜色设置不一致的问题,该问题主要是因为NavigationBar有透明度导致的,下面代码的方法比较实用:

@implementation
UINavigationBar (BackgroundColor)static char overlayKey; - (UIView *)overlay { return objc_getAssociatedObject(self, &overlayKey); } - (void)setOverlay:(UIView*)overlay{ objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)lt_setBackgroundColor:(UIColor *)backgroundColor { if (!self.overlay) { [self setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; [self setShadowImage:[[UIImage alloc] init]]; self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 64)];self.overlay.userInteractionEnabled = NO; [self insertSubview:self.overlayatIndex:0]; } self.overlay.backgroundColor = backgroundColor; }@end


然后在合适位置设置颜色即可:

- (void
)viewDidLoad { [super viewDidLoad]; self.PopDelegate =self.interactivePopGestureRecognizer.delegate; self.delegate = self; [self.navigationBar lt_setBackgroundColor:DefaultColorBlue]; [self.navigationBarsetTitleTextAttributes:@{NSForegroundColorAttributeName : DefaultColorWhite, NSFontAttributeName : [UIFont boldSystemFontOfSize:20]}]; [self.navigationBarsetTintColor:DefaultColorWhite]; }


第四点:解决push时隐藏Tabbar,这个就比较简单了,代码如下:

- (void
)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.viewControllers.count > 0) { viewController.hidesBottomBarWhenPushed = YES; } [superpushViewController:viewController animated:animated]; }


第五点:设置状态栏样式,代码如下:

- (UIStatusBarStyle
)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }


最后,我们可以根据需求进行一些通用设置,比如设置通用返回按钮:

- (void
)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { UIBarButtonItem *backBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"返回" style:UIBarButtonIt
97c9
emStylePlain target:selfaction:@selector(backBarButtonItemAction)]; viewController.navigationItem.backBarButtonItem = backBarButtonItem; } - (void)backBarButtonItemAction { [self popViewControllerAnimated:YES]; }


这样下来,我们就完成了一个比较实用的自定义的NavigationController的设置了,然后去自己的项目中使用吧。

                                                                                                                                             转载简书
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: