iOS UINavigationController adding left and right buttons does not display the problem solution

When we initialize a uinavigationcontroller with a uiviewcontroller, the left and right buttons are added again but not displayed, solution

one . Scenario simulation

We have a UINavigationController and a UIViewController, which are initialized when the program starts, the code is as follows
HomeViewController * homeCon = [[HomeViewController alloc] init]; // 初始化 uiviewcontroller
UINavigationHomeBar *homeNavigation = [[UINavigationHomeBar alloc] initWithRootViewController:homeCon];            // 初始化uinavigationcontroller

Next, when we add the left and right buttons to the uinavigationcontroller, there is no effect, the code is as follows
// This is in viewdidload of uinavigationcontroller
UIBarButtonItem *bill = [[UIBarButtonItem alloc] initWithTitle:@"账单" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItem = bill;

Run invisible button

two . Problem solving

1. Check the description of navigationitem first
@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.

Said: It is only created when the program needs it. If there is a viewcontroller, it may block the item in the current navigation.



2. Solution

Since the item in the uinavigationcontroller cannot be used, you need to use the item in the uiviewcontroller

to create a button and write it to the viewdidload of the uiviewcontroller middle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"title";
     
    // add bill button
    UIBarButtonItem *bill = [[UIBarButtonItem alloc] initWithTitle:@"账单" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.rightBarButtonItem = bill;
}

three. The solution

where Add a sentence to the viewdidload of the navigation
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

Just

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326861390&siteId=291194637