push & present
如果讓一個(gè)ViewController擁有navigationBar,有2種方式
-1- UINavigationController
這種方式是比較常見(jiàn)的一種简肴,也是最簡(jiǎn)單的一種晃听,但如果封裝起來(lái)一個(gè)controller供外部直接調(diào)用,就顯得有點(diǎn)不簡(jiǎn)潔了砰识。 ** 而且能扒,這種方式不支持push進(jìn)去**
比如:一個(gè)掃二維碼的page
(1) nav vc -- 供外部調(diào)用,是一個(gè)UINavigationController的子類
@interface QRCodeReaderViewController : UINavigationController
-(instancetype)initWithQRDelegate: (id)delegate;
@end
// 實(shí)現(xiàn)
-(instancetype)initWithQRDelegate: (id)delegate{
return [super initWithRootViewController: [[QRCodeReaderController alloc] initWithDelegate:delegate]];
}
(2) root vc -- 自己內(nèi)部封裝辫狼,掃碼功能實(shí)現(xiàn)
@interface QRCodeReaderController : UIViewController
-(instancetype)initWithQRDelegate: (id)delegate;
@end
// 核心類實(shí)現(xiàn)
-(instancetype)initWithQRDelegate:(id)delegate{
if (self = [super init]) {
_delegate = delegate;
}
return self;
}
這里很明顯就有了一個(gè)很雞肋的地方初斑,就是root vc里的接口方法,必須和nav vc一致膨处,不然就沒(méi)法初始化傳參數(shù)见秤,或進(jìn)行一系列直接的操作了
如果對(duì)外的接口方法有很多的話砂竖,在root vc里也要提供和nav vc一致的很多方法,顯得很累贅鹃答。
所以又發(fā)現(xiàn)了比較簡(jiǎn)潔的第二種方法乎澄。
-2- UIViewController + UINavigationBar
實(shí)現(xiàn)思路:[self.view addSubview:self.navigationBar]
簡(jiǎn)單說(shuō),就是將自定義的一個(gè)navigationBar作為self.view的一個(gè)subView测摔,模擬導(dǎo)航欄置济,好處是定制化程度高,可任意指定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; //可任意設(shè)置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
這種方法的好處是:
- 無(wú)論是以
presentViewController
還是pushViewController
的方式呈現(xiàn)舟肉,都不會(huì)影響你的navigationBar的定制化展現(xiàn)。
使用
- (IBAction)push:(id)sender {
[self.navigationController pushViewController:[TestViewController new] animated:true];
}
- (IBAction)present:(id)sender {
[self presentViewController:[TestViewController new] animated:true completion:nil];
}