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

Why is omnichannel marketing becoming crucial?

We all know that many years ago, in the era of TV...

What does a complete set of event operation planning ideas look like?

When I was chatting with you before, I made the f...

How to grill elegantly: Why does the smoke always chase me?

Author: New Media Center of Institute of Physics,...

Operation and promotion: Thoughts on the 3-fold growth of App users

Scenario—Creativity—Benefit Point—Cycle—Channel, ...

Experts suggest: Eat only 70% full for dinner! How full is "70% full"?

Expert of this article: Pa Lize, Chief Physician ...

Youth Physical Fitness Course Baidu Cloud Download

Youth Physical Fitness Course Resources Introduct...

How much does it cost to develop a luggage mini program in Alar?

WeChat Mini Program is an application that users ...

The top 10 misunderstandings in Tik Tok operations, have you fallen into them?

Before you started making short videos on TikTok,...

Labor Day is coming, here are ten sentences

May Day is coming, and the short holiday is comin...

Do you remember the 10 iPhone features that were cut by Apple?

After so many upgrades, do you still remember the...