推送viewController动画时UINavigationController上的子视图看起来不正确

时间:2012-11-07 19:08:42

标签: objective-c ios xcode

在我的UIViewController viewDidLoad中我有:

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

按钮显示就好了。不,我在堆栈上推送另一个viewcontroller:

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

当然Button仍然显示,所以我尝试将它隐藏在viewWillDisappear中并在viewWillAppear中显示它,但转换看起来并不正确。即使用隐藏的电话交换超级电话的顺序也不合适。

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

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

如何才能让这种过渡看起来正确?谢谢。

1 个答案:

答案 0 :(得分:0)

不使用按钮作为子视图,而是使用类似下面的内容

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