我有UINavigationItem
的自定义titleView,autoresizeMask
设置为调整视图的宽度和高度。如果我在可见设备的情况下旋转设备,则会自动调整较短横向导航栏的高度。但是,如果我按下一个新的视图控制器,旋转设备,然后返回到具有自定义titleView的设备,它无法识别高度的变化。现在我已将以下代码添加到viewWillAppear:
方法
- (void) viewWillAppear:(BOOL)animated {
CGRect frame = self.titleButton.frame;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
frame.size = CGSizeMake(self.navigationController.navigationBar.frame.size.width - 150.0f, 44.0f);
} else {
frame.size = CGSizeMake(self.navigationController.navigationBar.frame.size.width - 150.0f, 32.0f);
}
self.titleButton.frame = frame;
...
}
这解决了这个问题,但我想知道我是否错过了一些属性来让自己正确调整。自定义视图是UIButton:
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width - 150.0f, 44.0f)];
[[titleButton titleLabel] setFont:[UIFont boldSystemFontOfSize:18]];
[titleButton setBackgroundImage:navbarButtonImage forState:UIControlStateNormal];
[titleButton setBackgroundImage:navbarButtonPressedImage forState:UIControlStateSelected];
[titleButton setBackgroundImage:navbarButtonPressedImage forState:UIControlStateHighlighted];
titleButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleButton.autoresizesSubviews = YES;
有没有更好的方法来设置此视图?