如果让一个ViewController拥有navigationBar,有2种方式
-1- UINavigationController
这种方式是比较常见的一种,也是最简单的一种,但如果封装起来一个controller供外部直接调用,就显得有点不简洁了。 ** 而且,这种方式不支持push进去**
比如:一个扫二维码的page
(1) nav vc -- 供外部调用,是一个UINavigationController的子类
@interface QRCodeReaderViewController : UINavigationController
-(instancetype)initWithQRDelegate: (id)delegate;
@end
// 实现
-(instancetype)initWithQRDelegate: (id)delegate{
return [super initWithRootViewController: [[QRCodeReaderController alloc] initWithDelegate:delegate]];
}
(2) root vc -- 自己内部封装,扫码功能实现
@interface QRCodeReaderController : UIViewController
-(instancetype)initWithQRDelegate: (id)delegate;
@end
// 核心类实现
-(instancetype)initWithQRDelegate:(id)delegate{
if (self = [super init]) {
_delegate = delegate;
}
return self;
}
这里很明显就有了一个很鸡肋的地方,就是root vc里的接口方法,必须和nav vc一致,不然就没法初始化传参数,或进行一系列直接的操作了
如果对外的接口方法有很多的话,在root vc里也要提供和nav vc一致的很多方法,显得很累赘。
所以又发现了比较简洁的第二种方法。
-2- UIViewController + UINavigationBar
实现思路:[self.view addSubview:self.navigationBar]
简单说,就是将自定义的一个navigationBar作为self.view的一个subView,模拟导航栏,好处是定制化程度高,可任意指定frame。
核心代码:
//
// TestViewController.m
// UINavigationBarDemo
//
// Created by zhuxuhong on 16/9/6.
// Copyright © 2016年 zhuxuhong. All rights reserved.
//
#import "TestViewController.h"
typedef NS_ENUM(NSInteger, NavBarItemType) {
NavBarItemCancel,
NavBarItemOk,
NavBarItemRedo,
};
@interface TestViewController ()
@property(nonatomic, strong)UINavigationBar *navigationBar;
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self buildNavBar];
}
-(void)buildNavBar{
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview: self.navigationBar];
}
/**
* navigation bar
*/
-(UINavigationBar *)navigationBar{
if (!_navigationBar) {
if (self.navigationController) {
// 隐藏原有nav bar
[self.navigationController setNavigationBarHidden:true animated:true];
}
CGRect frame = self.view.bounds;
frame.size.height = 64; //可任意设置frame
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:frame];
bar.barStyle = UIBarStyleDefault;
bar.translucent = false;
bar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
bar.barTintColor = [UIColor redColor];
bar.tintColor = [UIColor whiteColor];
bar.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor] };
// bar button items
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(navBarButtonItemDidClick:)];
cancelItem.tag = NavBarItemCancel;
UIBarButtonItem *okItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(navBarButtonItemDidClick:)];
okItem.tag = NavBarItemOk;
UIBarButtonItem *redoItem = [[UIBarButtonItem alloc] initWithTitle:@"撤销" style:UIBarButtonItemStylePlain target:self action:@selector(navBarButtonItemDidClick:)];
redoItem.tag = NavBarItemRedo;
// nav item
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"UINavigationBar"];
navItem.leftBarButtonItem = cancelItem;
navItem.rightBarButtonItems = @[okItem, redoItem];
[bar pushNavigationItem:navItem animated:false];
_navigationBar = bar;
}
return _navigationBar;
}
// nav bar button item did click
-(void)navBarButtonItemDidClick: (UIBarButtonItem*)sender{
switch (sender.tag) {
case NavBarItemCancel:
[self backToPreviousPage];
break;
case NavBarItemOk:
[self backToPreviousPage];
break;
case NavBarItemRedo:
break;
default:
break;
}
}
/**
* back to previous page
*/
-(void)backToPreviousPage{
if (self.navigationController) {
[self.navigationController popViewControllerAnimated:true];
// 显示nav bar
[self.navigationController setNavigationBarHidden:false animated:false];
}
else{
[self dismissViewControllerAnimated:true completion:nil];
}
}
@end
这种方法的好处是:
- 无论是以
presentViewController
还是pushViewController
的方式呈现,都不会影响你的navigationBar的定制化展现。
使用
- (IBAction)push:(id)sender {
[self.navigationController pushViewController:[TestViewController new] animated:true];
}
- (IBAction)present:(id)sender {
[self presentViewController:[TestViewController new] animated:true completion:nil];
}