Modal View Controllers - UIBarButtonItems Do Not P

2019-08-28 22:16发布

I present modal view controllers, packaged in a UINavigationController.

When they first appear, the bar button items are alright.

However, if I click a button and a new view controller is pushed to the UINavigationController, and then I use the back button of that view controller, and it gets popped back to the original modal view controller, the buttons do not show up, (but they work when I click them -> just can't see them)

I can't seem to figure out why this occurs.

Here's some code.

Oh, and by the way, I customize the navigation controller's navigation bar's background. I have a feeling this may be causing this visual interference, but I don't know what to change to get it right.

In GGMainViewController:

GGSelectTeamsViewController *chooseTeam = [[GIFSelectTeamsViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:chooseTeam];
[self setupModalNavigationBar:navigationController];
[self presentViewController:navigationController animated:YES completion:nil];

- (void)setupModalNavigationBar:(UINavigationController *)nav {
    // unnecessary code removed for a quicker read
    // basically navigation bar has a background gradient as well as a bottom border
    [nav.navigationBar.layer addSublayer:bottomBorder];
    [nav.navigationBar.layer addSublayer:gradient];

}

In GGSelectTeamsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    title = [[UILabel alloc] init];
    title.text = someText;
    title.backgroundColor = [UIColor clearColor];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    [title sizeToFit];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    backButton.frame = someFrame;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton] animated:NO];


    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectButton setTitle:@"Select" forState:UIControlStateNormal];
    [selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    selectButton.frame = someFrame;
    [selectButton addTarget:self action:@selector(sendContent) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:selectButton] animated:NO];
}

Since the navigation items are set in the viewDidLoad method, why can't I see them (but they are still functional) when I return to it from another view controller?

2条回答
【Aperson】
2楼-- · 2019-08-28 22:51

That seems like an over-complicated way of doing this.

Why don't you create your own button and then use the setRightBarButtonItems:items: method from the UINavigationBar to set the buttons you want?

I'm guessing they disappear because you're adding them to the layer of the UINavigationBar.

Still, if you want to keep this method with the layers, you may want to add them in the viewWillAppear method, but you may need call the removeFromSuperlayer to make sure you don't add stuff more than once.

Hope this helps.

查看更多
Ridiculous、
3楼-- · 2019-08-28 23:12

The problem was:

  • the gradients for the navigation bar (done by code)

The solution was:

  • use Sketch to create an image of the gradients and use the appearance proxy to set the image as a background image

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];

查看更多
登录 后发表回答