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è)面的話(huà),有兩種方式:一 寫(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的UI開(kāi)發(fā)中Button的基本編寫(xiě)方法講解
這篇文章主要介紹了iOS的UI開(kāi)發(fā)中Button的基本編寫(xiě)方法講解,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
iOS開(kāi)發(fā)實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
iOS中自帶超強(qiáng)中文分詞器的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于iOS中自帶超強(qiáng)中文分詞器的實(shí)現(xiàn)方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
iOS如何獲取漢字(簡(jiǎn)體中文)筆畫(huà)數(shù)詳解
這篇文章主要給大家介紹了關(guān)于iOS如何獲取漢字(簡(jiǎn)體中文)筆畫(huà)數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
iOS開(kāi)發(fā)教程之UIView和UIViewController的生命周期詳解
UIViewController是IOS程序中的一個(gè)重要組成部分,下面這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之UIView和UIViewController的生命周期的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04

