Three ways to remove the back button text in the iOS navigation bar

Three ways to remove the back button text in the iOS navigation bar

[[403792]]

This article is reprinted from the WeChat public account "Wangluo Development", and the author is Jiejiao Yangwang. If you want to reprint this article, please contact the WeChat public account "Wangluo Development".

Solution 1

  1. Custom UINavigationController
  2. Comply with the ``` agreement
  3. Implement the following method:
  1. #pragma mark --------- UINavigationBarDelegate  
  2.  
  3. - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
  4.      
  5. //Set the navigation bar back button text
  6. UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action :nil];
  7. /*
  8. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  9. textAttrs[UITextAttributeTextColor] = [UIColor whiteColor];
  10. [back setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
  11. */
  12. item.backBarButtonItem = back;
  13.      
  14. return YES;
  15. }

Note: This method may cause a bug in the text of the return button on some sub-controller pages. You need to set the return button again in the parent controller of the sub-controller page as above.

  1. The parent controller of the child controller page
  2.  
  3. #pragma mark -------- life cycle function  
  4.  
  5. - (void)viewDidLoad {
  6. [super viewDidLoad];
  7. // Do any additional setup after loading the view .
  8.      
  9. self. view .backgroundColor = [UIColor whiteColor];
  10.      
  11. //Reset the text of the return button in the navigation bar of the lower-level sub-page
  12. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action :nil];
  13. self.navigationItem.backBarButtonItem = item;
  14.  
  15. }

Solution 2

  1. Custom UINavigationController
  2. obey protocol
  3. Implement the following method:
  1. #pragma mark --------- UINavigationBarDelegate  
  2.  
  3. - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
  4.      
  5. //Set the navigation bar back button text to transparent, which may cause the navigation title to be off-center
  6. [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
  7. [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
  8.      
  9. return YES;
  10. }

Option 3 (recommended)

  1. Add a category to UIViewController (the category here does not need to be imported and can be used directly)
  2. Then replace the ViewDidAppear method with the Method Swzilling method in the load method. The code is as follows:
  1. #pragma mark --------- UINavigationBarDelegate  
  2.  
  3. - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
  4.      
  5. //Set the navigation bar back button text to transparent, which may cause the navigation title to be off-center
  6. [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
  7. [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
  8.      
  9. return YES;
  10. }

<<:  Vulgar content can still be found! Is the App's "Teen Mode" just for show?

>>:  Coming next week! iOS 15 details revealed: improved multitasking, Apple's new notification banner design

Recommend

Tik Tok video copywriting operation skills tutorial

Douyin video copywriting operation skills tutoria...

4 core basic skills that marketers must know!

Marketing insights: Marketing is a means of satis...

Virtual Reality to Reach $30 Billion by 2020

According to a recent report from Digi-Capital, t...

How to Evolve Neural Networks with Automatic Machine Learning

For most people working in machine learning, desi...

Dell's all-in acquisition of EMC causes 10 shocks

[[151859]] Text/Lao Liang on the front line Hopef...

5000 words to analyze the philosophy of planting grass when browsing Taobao!

It’s Double Eleven again this year. At 0:00 on No...

Why are most organisms "symmetrical"?

Today, the biodiversity of the earth is very rich...

What should I do if I spend money on Baidu promotion but there is no effect?

I believe that many business owners or managers w...

Teach you to write a super simple swoole chat room in five minutes

I was originally planning to continue writing my ...

What are the services provided by PPT production companies?

In our daily work, we need to use various types o...