導(dǎo)航欄 UINavigation

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è)

![customItem.png](http://upload-images.jianshu.io/upload_images/3141335-660bb839711b436f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


同理可以修改leftBarButtonItem 和 隱藏返回鍵。
https://github.com/hardy88/HTStudy/tree/master/iOS學(xué)習(xí)/HTHXBB
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容