简体   繁体   中英

Subview on UINavigationController when pushing viewController animated doesn't look right

In my UIViewController viewDidLoad I have:

myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
...
myButton.frame = CGRectMake(0, 2, 40, 40);
[self.navigationController.navigationBar addSubview:myButton];

Button shows just fine. No I push another viewcontroller on the stack:

 [self.navigationController pushViewController:vc animated:YES];

Of course the Button is still showing so I try hiding it in the viewWillDisappear and showing it in the viewWillAppear but the transitions just doesn't look right. Even swapping the order of the super calls with the hidden calls doesn't look right.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    myButton.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    myButton.hidden = NO;
    [super viewWillAppear:animated];
}

What can I do to get this transition to look right? Thank you.

Instead of adding the button as subview use something like the following

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:YourCustomView];
self.navigationItem.rightBarButtonItem = barButton;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM