UINavigation
系統(tǒng)默認(rèn)從上一個(gè)ViewController跳轉(zhuǎn)到下一個(gè)ViewController時(shí),backItem 是 < + 上ViewController的Navigation title這樣的,如

backItem.png
但項(xiàng)目可能會(huì)被要求所有的Vc界面的返回按鈕為

backItemCustom.png
這時(shí)我們可以改寫系統(tǒng)的PushViewController方法,在頁(yè)面被Push過(guò)去時(shí),將BackItem換成我們自己定義的item。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0)
{
viewController.hidesBottomBarWhenPushed = YES;
// 設(shè)置下一頁(yè)面的返回鍵
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
but.frame = CGRectMake(0, 0, 50, 20);
[but setImage:[UIImage imageNamed:@"nav_back_black.png"] forState:UIControlStateNormal];
[but setTitle:@"<返回" forState:UIControlStateNormal];
but.userInteractionEnabled = YES;
[but addTarget:self action:@selector(toBack) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:but];
}
[super pushViewController:viewController animated:animated];
}
- (void)toBack
{
[self popViewControllerAnimated:YES];
}
在我們自己寫的toBack方法里面調(diào)用系統(tǒng)的popViewControllerAnimated方法,這樣就實(shí)現(xiàn)了所有的頁(yè)面都是統(tǒng)一的返回風(fēng)格。
UIBarButtonItem初始化方法中提供了
- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
我們用自己寫好的selctor去替換到系統(tǒng)的rightBarButtonItem,那么就可以實(shí)現(xiàn)將我們自己想要的東西添加到導(dǎo)航欄上
self.navigationItem.rightBarButtonItem = item; ```
在toNext方法中實(shí)現(xiàn)我們自己的操作
- (void)toNext
{
// 自己的實(shí)現(xiàn)
};
如下圖,點(diǎn)擊“完成”后就會(huì)pop回上一頁(yè)

同理可以修改leftBarButtonItem 和 隱藏返回鍵。
https://github.com/hardy88/HTStudy/tree/master/iOS學(xué)習(xí)/HTHXBB