1

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;

有没有更好的方法来设置这个视图?

4

0 回答 0