自定义navigation(导航栏)

来源:互联网 发布:阿尔法收益 知乎 编辑:程序博客网 时间:2024/05/21 09:53

(1)隐藏系统默认的导航栏:

自定义一个NavigationController,在这个类里只做一件事情,那就是隐藏系统默认的导航栏,代码如下:
[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. - (void)awakeFromNib {  
  2.     [super awakeFromNib];  
  3.     // 隐藏默认导航栏  
  4.     self.navigationBarHidden = NO// 使右滑返回手势可用  
  5.     self.navigationBar.hidden = YES// 隐藏导航栏  
  6. }  
因为我是在storyboard中实现的,所以在awakeFromNib方法中隐藏默认的导航栏。
另外需要注意的是:不能直接用self.navigationBarHidden = YES来隐藏导航栏,因为用这个属性的话,系统默认的右滑返回手势就没有了,切记,一定要用self.navigationBar.hidden = YES来隐藏,并将self.navigationBarHidden的值设为YES(默认就是YES,所以可以不用加这句)。

(2)自定义一个NavigationBar:

根据自己的需求,自定义一个NavigationBar,我在这里自定义了一个UIView的子类,叫YSNavigationBar,用Xib实现的,我把这个View分成了3部分,左边的按钮,中间的标题,右侧的按钮,模拟一下系统默认的导航栏,实现起来也很简单,代码如下:

YSNavigationBar.h:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  YSNavigationBar.h  
  3. //  自定义NavigationBar_01  
  4. //  
  5. //  Created by 张延深 on 16/2/2.  
  6. //  Copyright © 2016年 宜信. All rights reserved.  
  7. //  自定义的导航栏  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. typedef void(^NavBtnClickBlock)();  
  12.   
  13. @interface YSNavigationBar : UIView  
  14.   
  15. @property (weak, nonatomic) IBOutlet UIView *leftView;  
  16. @property (weak, nonatomic) IBOutlet UIView *rightView;  
  17. @property (weak, nonatomic) IBOutlet UIView *centerView;  
  18.   
  19. @property (weak, nonatomic) IBOutlet UIButton *leftBtn;  
  20. @property (weak, nonatomic) IBOutlet UIButton *rightBtn;  
  21. @property (weak, nonatomic) IBOutlet UILabel *centerLbl;  
  22.   
  23. @property (nonatomiccopy) NavBtnClickBlock leftBtnClickHandler;  
  24. @property (nonatomiccopy) NavBtnClickBlock rightBtnClickHandler;  
  25.   
  26. @end  

YSNavigationBar.m:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  YSNavigationBar.m  
  3. //  自定义NavigationBar_01  
  4. //  
  5. //  Created by 张延深 on 16/2/2.  
  6. //  Copyright © 2016年 宜信. All rights reserved.  
  7. //  
  8.   
  9. #import "YSNavigationBar.h"  
  10.   
  11. @implementation YSNavigationBar  
  12.   
  13. - (void)awakeFromNib {  
  14.     [super awakeFromNib];  
  15.     // 初始化界面  
  16.     [self initAllView];  
  17. }  
  18.   
  19. - (void)initAllView {  
  20.     self.backgroundColor = [UIColor greenColor];  
  21.     self.leftView.backgroundColor = [UIColor clearColor];  
  22.     self.centerView.backgroundColor = [UIColor clearColor];  
  23.     self.rightView.backgroundColor = [UIColor clearColor];  
  24.     // 默认右侧按钮隐藏  
  25.     self.rightView.hidden = YES;  
  26. }  
  27.   
  28. #pragma mark - event response  
  29.   
  30. - (IBAction)leftBtnClick:(UIButton *)sender {  
  31.     if (_leftBtnClickHandler) {  
  32.         _leftBtnClickHandler();  
  33.     }  
  34. }  
  35.   
  36. - (IBAction)rightBtnClick:(UIButton *)sender {  
  37.     if (_rightBtnClickHandler) {  
  38.         _rightBtnClickHandler();  
  39.     }  
  40. }  
  41.   
  42. @end  

YSNavigationBar.xib:


(3)在原来导航栏的位置放置自定义的NavigationBar:

现在默认的导航栏已经隐藏了,自定义的导航栏也有了,下面就要进行最后一步了,那就是在原来导航栏的位置放置自定义的NavigationBar。

因为可能会有很多页面需要自定义导航栏,所以我们需要一个父类,所有需要自定义导航栏的VC都继承它。自定义一个UIViewController的子类YSBaseViewController,将这个类作为所有需要自定义导航栏的VC的父类,代码如下:

YSBaseViewController.h:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  YSBaseViewController.h  
  3. //  自定义NavigationBar_01  
  4. //  
  5. //  Created by 张延深 on 16/2/2.  
  6. //  Copyright © 2016年 宜信. All rights reserved.  
  7. //  ViewController的基类,想要自定义导航栏的继承它。  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @class YSNavigationBar;  
  12.   
  13. @interface YSBaseViewController : UIViewController  
  14.   
  15. @property (nonatomiccopyNSString *titleStr; // 导航栏标题  
  16.   
  17. @property (nonatomic, assign) BOOL leftButtonHidden;  
  18. @property (nonatomic, assign) BOOL rightButtonHidden;  
  19.   
  20. @property (nonatomicstrongUIColor *navBgColor; // 导航栏背景色  
  21.   
  22. @property (readonlynonatomicstrongUIButton *leftButton;  
  23. @property (readonlynonatomicstrongUIButton *rightButton;  
  24. @property (readonlynonatomicstrongUILabel *centerLabel;  
  25.   
  26. - (void)replaceDefaultNavBar:(UIView *)nav;  
  27.   
  28. @end  

YSBaseViewController.m:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  YSBaseViewController.m  
  3. //  自定义NavigationBar_01  
  4. //  
  5. //  Created by 张延深 on 16/2/2.  
  6. //  Copyright © 2016年 宜信. All rights reserved.  
  7. //  
  8.   
  9. #import "YSBaseViewController.h"  
  10. #import "YSNavigationBar.h"  
  11. #import "UIView+UIViewCreateFromXib.h"  
  12.   
  13. @interface YSBaseViewController ()  
  14.   
  15. @property (nonatomicstrongYSNavigationBar *navigationBar; // 自定义的导航栏  
  16.   
  17. @end  
  18.   
  19. @implementation YSBaseViewController  
  20.   
  21. - (void)viewDidLoad {  
  22.     [super viewDidLoad];  
  23.     // 初始化导航栏  
  24.     [self initNavigationBar];  
  25. }  
  26.   
  27. #pragma mark - getter/setter  
  28.   
  29. - (void)setTitleStr:(NSString *)titleStr {  
  30.     _titleStr = titleStr;  
  31.     self.centerLabel.text = titleStr;  
  32. }  
  33.   
  34. - (void)setLeftButtonHidden:(BOOL)leftButtonHidden {  
  35.     _leftButtonHidden = leftButtonHidden;  
  36.     self.leftButton.hidden = leftButtonHidden;  
  37. }  
  38.   
  39. - (void)setRightButtonHidden:(BOOL)rightButtonHidden {  
  40.     _rightButtonHidden = rightButtonHidden;  
  41.     _navigationBar.rightView.hidden = rightButtonHidden;  
  42. }  
  43.   
  44. - (void)setNavBgColor:(UIColor *)navBgColor {  
  45.     _navBgColor = navBgColor;  
  46.     _navigationBar.backgroundColor = navBgColor;  
  47. }  
  48.   
  49. - (UIButton *)leftButton {  
  50.     NSAssert(_navigationBar, @"_navigationBar == nil");  
  51.     if (_navigationBar) {  
  52.         return _navigationBar.leftBtn;  
  53.     }  
  54.     return nil;  
  55. }  
  56.   
  57. - (UIButton *)rightButton {  
  58.     NSAssert(_navigationBar, @"_navigationBar == nil");  
  59.     if (_navigationBar) {  
  60.         return _navigationBar.rightBtn;  
  61.     }  
  62.     return nil;  
  63. }  
  64.   
  65. - (UILabel *)centerLabel {  
  66.     NSAssert(_navigationBar, @"_navigationBar == nil");  
  67.     if (_navigationBar) {  
  68.         return _navigationBar.centerLbl;  
  69.     }  
  70.     return nil;  
  71. }  
  72.   
  73. #pragma mark - public methods  
  74.   
  75. - (void)replaceDefaultNavBar:(UIView *)nav {  
  76.     NSAssert(nav, @"nav == nil");  
  77.     if (nav) {  
  78.         [_navigationBar removeFromSuperview];  
  79.         _navigationBar = nil;  
  80.         [self.view addSubview:nav];  
  81.         [self addConstraintsWithView:nav];  
  82.     }  
  83. }  
  84.   
  85. #pragma mark - private methods  
  86.   
  87. - (void)initNavigationBar {  
  88.     if (!_navigationBar) {  
  89.         _navigationBar = [YSNavigationBar createViewFromXib];  
  90.         // 这句话必须加上,否则约束就有问题  
  91.         _navigationBar.translatesAutoresizingMaskIntoConstraints = NO;  
  92.     }  
  93.     [self.view addSubview:_navigationBar];  
  94.     [self addConstraintsWithView:_navigationBar];  
  95.     if (self.navigationController) {  
  96.         if (self.navigationController.viewControllers[0] == self) {  
  97.             self.leftButton.hidden = YES;  
  98.         } else {  
  99.             self.leftButton.hidden = NO;  
  100.         }  
  101.     }  
  102.     // 默认左侧返回按钮  
  103.     [self.leftButton setImage:[UIImage imageNamed:@"YSBackBtn"] forState:UIControlStateNormal];  
  104.     [self.leftButton setTitle:@"" forState:UIControlStateNormal];  
  105.     __weak typeof(self) weakSelf = self;  
  106.     // 给左侧按钮添加默认事件  
  107.     _navigationBar.leftBtnClickHandler = ^() {  
  108.         __strong typeof(self) strongSelf = weakSelf;  
  109.         [strongSelf defaultLeftBtnClick];  
  110.     };  
  111.     // 给右侧按钮添加默认事件  
  112.     _navigationBar.rightBtnClickHandler = ^() {  
  113.         __strong typeof(self) strongSelf = weakSelf;  
  114.         [strongSelf defaultRightBtnClick];  
  115.     };  
  116. }  
  117.   
  118. #pragma mark 给自定义导航栏添加约束  
  119. - (void)addConstraintsWithView:(UIView *)view {  
  120.     // top  
  121.     NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];  
  122.     // left  
  123.     NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];  
  124.     // right  
  125.     NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];  
  126.     [self.view addConstraints:@[topConstraint, leftConstraint, rightConstraint]];  
  127.     // height  
  128.     NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:view.bounds.size.height];  
  129.     [view addConstraint:heightConstraint];  
  130. }  
  131.   
  132. #pragma mark 子类可以重写来实现不同的功能  
  133. - (void)defaultLeftBtnClick {  
  134.     NSAssert(self.navigationController@"self.navigationController == nil");  
  135.     if (self.navigationController) {  
  136.         [self.navigationController popViewControllerAnimated:YES];  
  137.     }  
  138. }  
  139.   
  140. - (void)defaultRightBtnClick {  
  141.       
  142. }  
  143.   
  144. @end  
到这里,我们的工作基本已经完成,下面需要做的就是继承YSBaseViewController这个类,就OK了!

因为UIView默认是不能直接创建Xib文件的,所以我在这里写了一个UIView的Category,用来加载Xib,代码如下:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  UIView+UIViewCreateFromXib.m  
  3. //  自定义NavigationBar_01  
  4. //  
  5. //  Created by 张延深 on 16/2/2.  
  6. //  Copyright © 2016年 宜信. All rights reserved.  
  7. //  
  8.   
  9. #import "UIView+UIViewCreateFromXib.h"  
  10.   
  11. @implementation UIView (UIViewCreateFromXib)  
  12.   
  13. + (instancetype)createViewFromXib {  
  14.     NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:self options:nil];  
  15.     NSAssert(nibs && nibs.count != 0@"获取Nib失败!");  
  16.     return nibs[0];  
  17. }  
  18.   
  19. @end  
0 0
原创粉丝点击