iOS 11 UINavigation Bar搞砸了

时间:2017-10-02 09:39:57

标签: ios objective-c xcode ios11

iOS 11导航栏的标题视图和条形按钮项目不居中。 此外,背景图像的高度不会改变,也不会完整显示。条形高度为74。

enter image description here 看白色空间。

我试过这个

if(@available(iOS 11,*)){
    _homeNavigationBar.prefersLargeTitles = NO;
    _homeNavigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
    [_homeNavigationBar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbarBg.png"]]];
}
else{
    [_homeNavigationBar setBackgroundImage:[UIImage imageNamed:@"navbarBg.png"] forBarMetrics:UIBarMetricsDefault];
}

但我仍然无法将标题和栏按钮项目居中。

我知道如何解决这个问题?请让我知道。谢谢

1 个答案:

答案 0 :(得分:0)

对导航栏进行子类化对我来说很有用。

- (void)layoutSubviews {
[super layoutSubviews];

for (UIView *view in self.subviews) {
    if([NSStringFromClass([view class]) containsString:@"Background"]) {
        view.frame = self.bounds;
    }
    else if ([NSStringFromClass([view class]) containsString:@"ContentView"]) {
        CGRect frame = view.frame;
        frame.origin.y = 25;
        view.frame = frame;
    }
 }
}