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

Apple Watch UI Animation Analysis

After watching the keynote of Apple Watch, we wer...

Audi A3 nitrogen oxide emissions may exceed European standards by double

According to Reuters, in laboratory tests conduct...

iOS8 hidden feature: Unicom version of iPhone 5 uses Telecom 3G

Apple released iOS8 Beta1 in the early morning of ...

Shanghai server cabinet rental price

The parameter configuration of Shanghai Data Cent...

Don't know what to do with your kids? Why not look for spring bugs?

Audit expert: Li Weiyang Well-known science write...

Snapdragon 820 version of Samsung S7 latest running score exposed: a new high

As the release is approaching, Samsung S7 is also...

How was the fight between Kris Wu and Hupu "planned" by marketing?

Starting from the 25th, #吴亦凡虎扑# began to dominate...

5 Steps to Write a Hot Article with 100,000+ Reads (Part 2)

The main thread of this article is the content and...