iOS 11 navigation bar height customizing

后端 未结 11 847
无人共我
无人共我 2020-11-28 01:27

Now in iOS 11, the sizeThatFits method is not called from UINavigationBar subclasses. Changing the frame of UINavigationBar causes gli

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 01:51

    Although it's fixed in beta 4, it seems the background image of the nav bar does not scale with the actual view (you can verify this by looking at at in the view-hierarchy viewer). A workaround for now is to override layoutSubviews in your custom UINavigationBar and then use this code:

    - (void)layoutSubviews
    {
      [super layoutSubviews];
    
      for (UIView *subview in self.subviews) {
        if ([NSStringFromClass([subview class]) containsString:@"BarBackground"]) {
            CGRect subViewFrame = subview.frame;
            subViewFrame.origin.y = -20;
            subViewFrame.size.height = CUSTOM_FIXED_HEIGHT+20;
            [subview setFrame: subViewFrame];
        }
      }
    }
    

    If you notice, the bar background in fact has an offset of -20 to make it appear behind the status bar, so the calculation above adds that in.

提交回复
热议问题