去掉 iOS 導(dǎo)航欄返回按鈕文本三種方案
作者:街角仰望
方案一:該方法會(huì)出現(xiàn)部分子控制器頁(yè)面的返回按鈕文字出現(xiàn)的bug,需要在其子控制器頁(yè)面的父控制器里再次如上設(shè)置返回按鈕才行。
本文轉(zhuǎn)載自微信公眾號(hào)「網(wǎng)羅開(kāi)發(fā)」,作者街角仰望。轉(zhuǎn)載本文請(qǐng)聯(lián)系網(wǎng)羅開(kāi)發(fā)公眾號(hào)。
方案一
- 自定義 UINavigationController
- 遵守 ``` 協(xié)議
- 實(shí)現(xiàn)下面方法:
- #pragma mark --------- UINavigationBarDelegate
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
- //設(shè)置導(dǎo)航欄返回按鈕文字
- UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
- /*
- NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
- textAttrs[UITextAttributeTextColor] = [UIColor whiteColor];
- [back setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
- */
- item.backBarButtonItem = back;
- return YES;
- }
注意:該方法會(huì)出現(xiàn)部分子控制器頁(yè)面的返回按鈕文字出現(xiàn)的bug,需要在其子控制器頁(yè)面的父控制器里再次如上設(shè)置返回按鈕才行
- 子控制器頁(yè)面的父控制器
- #pragma mark -------- 生命周期函數(shù)
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.view.backgroundColor = [UIColor whiteColor];
- //重新設(shè)置下級(jí)子頁(yè)面導(dǎo)航欄返回按鈕文字
- UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
- self.navigationItem.backBarButtonItem = item;
- }
方案二
- 自定義 UINavigationController
- 遵守
協(xié)議 - 實(shí)現(xiàn)下面方法:
- #pragma mark --------- UINavigationBarDelegate
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
- //設(shè)置導(dǎo)航欄返回按鈕文字為透明的,可能造成導(dǎo)航標(biāo)題不居中的問(wèn)題
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
- return YES;
- }
方案三(推薦)
- 給 UIViewController 添加類(lèi)別(這里的類(lèi)別不需要導(dǎo)入可直接使用)
- 然后在 load 方法里面用 Method Swzilling 方法替換交換 ViewDidAppear 方法,代碼如下:
- #pragma mark --------- UINavigationBarDelegate
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
- //設(shè)置導(dǎo)航欄返回按鈕文字為透明的,可能造成導(dǎo)航標(biāo)題不居中的問(wèn)題
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
- return YES;
- }
責(zé)任編輯:武曉燕
來(lái)源:
網(wǎng)羅開(kāi)發(fā)