[[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- Custom UINavigationController
- Comply with the ``` agreement
- Implement the following method:
- #pragma mark
-
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
-
- //Set the navigation bar back button text
- UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action :nil];
- /*
- NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
- textAttrs[UITextAttributeTextColor] = [UIColor whiteColor];
- [back setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
- */
- item.backBarButtonItem = back;
-
- return YES;
- }
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. - The parent controller of the child controller page
-
- #pragma mark
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view .
-
- self. view .backgroundColor = [UIColor whiteColor];
-
- //Reset the text of the return button in the navigation bar of the lower-level sub-page
- UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action :nil];
- self.navigationItem.backBarButtonItem = item;
-
- }
Solution 2- Custom UINavigationController
- obey protocol
- Implement the following method:
- #pragma mark
-
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
-
- //Set the navigation bar back button text to transparent, which may cause the navigation title to be off-center
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
-
- return YES;
- }
Option 3 (recommended)- Add a category to UIViewController (the category here does not need to be imported and can be used directly)
- Then replace the ViewDidAppear method with the Method Swzilling method in the load method. The code is as follows:
- #pragma mark
-
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
-
- //Set the navigation bar back button text to transparent, which may cause the navigation title to be off-center
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
- [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
-
- return YES;
- }
|