iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例
當(dāng)一個(gè)項(xiàng)目發(fā)現(xiàn)每個(gè)返回的按鈕都是一樣的,并且標(biāo)題的字體也不是系統(tǒng)的字體,如果每個(gè)頁(yè)面都去設(shè)置返回按鈕,重新設(shè)置標(biāo)題字體,這樣代碼看著繁雜,而且會(huì)浪費(fèi)很多時(shí)間,這時(shí)候就有必要封裝一下了。。。
首先返回按鈕,需要在當(dāng)前頁(yè)面pop 到上一個(gè)頁(yè)面的話,有兩種方式:一 寫(xiě)一個(gè)點(diǎn)擊代理,在用到的頁(yè)面實(shí)現(xiàn)它,二 就是獲取button所在的當(dāng)前控制器,然后pop出去。 但是第一個(gè)方法,還需要到用到的頁(yè)面去實(shí)現(xiàn)代理,也比較麻煩,那就來(lái)說(shuō)第二種
首先獲取當(dāng)前控制器的方法:
UINavigationController *vc = [[UINavigationController alloc] init]; for (UIView* next = [sender superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { vc = (UINavigationController*)nextResponder; [vc.topViewController.navigationController popViewControllerAnimated:YES]; return; } }
因?yàn)槲疫@里的按鈕在navigationController上所以,這里的控制器變量都是 UINavigationController,如果需要獲取的是一般的UIViewController,那就把上面所有的UINavigationController 改成 UIViewController
獲取完之后,我們就使用這個(gè)來(lái)封裝自己的簡(jiǎn)單的導(dǎo)航欄,示例代碼:
+ (void)setNavigationBarWithTitle:(NSString *)title controller:(UIViewController *)controller{ controller.title = title; [controller.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName:kMainTextColor,NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Light" size:18]}]; //返回按鈕 UIButton *btn = [[UIButton alloc] init]; [btn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)]; [btn setTitleColor:kMainTextColor forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:13]; [btn addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)]; controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; } + (void)back:(UIButton *)sender{ UINavigationController *vc = [[UINavigationController alloc] init]; for (UIView* next = [sender superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { vc = (UINavigationController*)nextResponder; [vc.topViewController.navigationController popViewControllerAnimated:YES]; return; } } }
以上這篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
iOS自定義雷達(dá)掃描擴(kuò)散動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了iOS自定義雷達(dá)掃描擴(kuò)散動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10iOS 9 更新之Safari廣告攔截器(Content Blocker)開(kāi)發(fā)教程
這篇文章主要介紹了iOS 9 更新之Safari廣告攔截器(Content Blocker)開(kāi)發(fā)教程的相關(guān)資料,需要的朋友可以參考下2015-08-08Flutter列表滾動(dòng)定位超強(qiáng)輔助庫(kù)使用示例詳解
這篇文章主要為大家介紹了Flutter列表滾動(dòng)定位超強(qiáng)輔助庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08iOS在頁(yè)面銷毀時(shí)如何優(yōu)雅的cancel網(wǎng)絡(luò)請(qǐng)求詳解
這篇文章主要給大家介紹了關(guān)于iOS在頁(yè)面銷毀時(shí)如何優(yōu)雅的cancel網(wǎng)絡(luò)請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-0512個(gè)iOS技術(shù)面試題及答案總結(jié)
這篇文章給大家總結(jié)了在iOS面試的時(shí)候可能會(huì)遇到的12個(gè)技術(shù)面試題,以及這些面試題但答案,這些答案只是給大家一些參考,大家可以再結(jié)合自己理解進(jìn)行回答,有需要的朋友們下面來(lái)一起看看吧。2016-09-09Android NavigationController 右滑手勢(shì)詳解
目前蘋(píng)果手機(jī)在人機(jī)交互中盡力做到極致,在ios7中,新增了一個(gè)小小功能,用戶不用點(diǎn)擊右上角的返回按鈕,在屏幕左邊一滑,就會(huì)返回。下面給大家詳解Android NavigationController 右滑手勢(shì),需要的朋友可以參考下2015-08-08