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

Product Promotion: 11 Ways to Make Your Product Popular

Why is it that even though some products are rare...

iQiyi advertising account opening, iQiyi promotion billing

The average online time of iQiyi users is long an...

4 super practical techniques to attract traffic and increase followers!

The so-called traffic diversion means that there ...

Is it true that Pinduoduo video can withdraw money? How to play?

Now many e-commerce platforms, including short vi...

Crystal Energy Healing Book Baidu Cloud Download

Crystal Energy Healing Manual Recommended by all ...

Double No. 1! Perfect ending! This time we really have to say goodbye...

Screenshot from the Beijing Winter Paralympic Gam...

Ganfanren Yiyi_How to create a vertical account for beginners video

The course from Ganfanren Yiyi teaches beginners ...

Unveiling the mystery of runtime

[[164970]] I recently wanted to change jobs, and ...

How to promote an app without any budget?

This article is compiled from Zhihu: Zhihu user: ...