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

Uncle Wolf's Toutiao Traffic Drainage Technology 9th Course Video

Course Outline 1. The traffic-generating advantag...

3 Principles of To B Case Marketing

Case marketing is a very practical topic that To ...

Where does the poop on the plane go?

This issue is planned by: Doraemon, Little Dandel...

What are competing products? 8 ways to collect competitive products

First of all, what is a competitor? Why do we nee...

The world's thinnest pasta is born! Researchers say: It's inedible

Compiled by: Gong Zixin The world's thinnest ...

How to operate a good community? Share 2 tips!

What I want to share today is how to do a good jo...