iOS 11 navigation bar height customizing

后端 未结 11 758
无人共我
无人共我 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.

    0 讨论(0)
  • 2020-11-28 01:51

    This is what I use. It works for regular content (44.0 px) if you use UISearchBar as title or other views that modify the size of the bar content, you must update the values accordingly. Use this at your own risk since it might brake at some point.

    This is the navbar with 90.0px height hardcoded, working on both iOS 11 and older versions. You might have to add some insets to the UIBarButtonItem for pre iOS 11 to look the same.

    class NavBar: UINavigationBar {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            if #available(iOS 11, *) {
                translatesAutoresizingMaskIntoConstraints = false
            }
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func sizeThatFits(_ size: CGSize) -> CGSize {
            return CGSize(width: UIScreen.main.bounds.width, height: 70.0)
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            guard #available(iOS 11, *) else {
                return
            }
    
            frame = CGRect(x: frame.origin.x, y:  0, width: frame.size.width, height: 90)
    
            if let parent = superview {
                parent.layoutIfNeeded()
    
                for view in parent.subviews {
                    let stringFromClass = NSStringFromClass(view.classForCoder)
                    if stringFromClass.contains("NavigationTransition") {
                        view.frame = CGRect(x: view.frame.origin.x, y: frame.size.height - 64, width: view.frame.size.width, height: parent.bounds.size.height - frame.size.height + 4)
                    }
                }
            }
    
            for subview in self.subviews {
                var stringFromClass = NSStringFromClass(subview.classForCoder)
                if stringFromClass.contains("BarBackground") {
                    subview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 90)
                    subview.backgroundColor = .yellow
                }
    
                stringFromClass = NSStringFromClass(subview.classForCoder)
                if stringFromClass.contains("BarContent") {
                    subview.frame = CGRect(x: subview.frame.origin.x, y: 40, width: subview.frame.width, height: subview.frame.height)
    
                }
            }
        }
    }
    

    And you add it to a UINavigationController subclass like this:

    class CustomBarNavigationViewController: UINavigationController {
    
        init() {
            super.init(navigationBarClass: NavBar.self, toolbarClass: nil)
        }
    
        override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        }
    
        override init(rootViewController: UIViewController) {
            super.init(navigationBarClass: NavBar.self, toolbarClass: nil)
    
            self.viewControllers = [rootViewController]
        }
    
        required public init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 01:53

    Added: The problem is solved in iOS 11 beta 6 ,so the code below is of no use ^_^


    Original answer:

    Solved with code below :

    (I always want the navigationBar.height + statusBar.height == 64 whether the hidden of statusBar is true or not)

     @implementation P1AlwaysBigNavigationBar
    
    - (CGSize)sizeThatFits:(CGSize)size {
        CGSize sizeThatFit = [super sizeThatFits:size];
        if ([UIApplication sharedApplication].isStatusBarHidden) {
            if (sizeThatFit.height < 64.f) {
                sizeThatFit.height = 64.f;
            }
        }
        return sizeThatFit;
    }
    
    - (void)setFrame:(CGRect)frame {
        if ([UIApplication sharedApplication].isStatusBarHidden) {
            frame.size.height = 64;
        }
        [super setFrame:frame];
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        if (![UIApplication sharedApplication].isStatusBarHidden) {
            return;
        }
    
        for (UIView *subview in self.subviews) {
            NSString* subViewClassName = NSStringFromClass([subview class]);
            if ([subViewClassName containsString:@"UIBarBackground"]) {
                subview.frame = self.bounds;
            }else if ([subViewClassName containsString:@"UINavigationBarContentView"]) {
                if (subview.height < 64) {
                    subview.y = 64 - subview.height;
                }else {
                    subview.y = 0;
                }
            }
        }
    }
    @end
    
    0 讨论(0)
  • 2020-11-28 01:55

    on Xcode 9 Beta 6 I still have the issue. The Bar always looks 44 pixel height and it is pushed under the status bar.

    In order to solve that I made a subclass with @strangetimes code (in Swift)

    class NavigationBar: UINavigationBar {
    
      override func layoutSubviews() {
        super.layoutSubviews()
    
        for subview in self.subviews {
          var stringFromClass = NSStringFromClass(subview.classForCoder)
          print("--------- \(stringFromClass)")
          if stringFromClass.contains("BarBackground") {
            subview.frame.origin.y = -20
            subview.frame.size.height = 64
          }
        }
      }
    }
    

    and I place the bar lower than the status bar

    let newNavigationBar = NavigationBar(frame: CGRect(origin: CGPoint(x: 0,
                                                                           y: 20),
                                                             size: CGSize(width: view.frame.width,
                                                                          height: 64)
          )
        ) 
    
    0 讨论(0)
  • 2020-11-28 01:59

    Simplified with Swift 4.

    class CustomNavigationBar : UINavigationBar {
    
        private let hiddenStatusBar: Bool
    
        // MARK: Init
        init(hiddenStatusBar: Bool = false) {
            self.hiddenStatusBar = hiddenStatusBar
            super.init(frame: .zero)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        // MARK: Overrides
        override func layoutSubviews() {
            super.layoutSubviews()
    
            if #available(iOS 11.0, *) {
                for subview in self.subviews {
                    let stringFromClass = NSStringFromClass(subview.classForCoder)
                    if stringFromClass.contains("BarBackground") {
                        subview.frame = self.bounds
                    } else if stringFromClass.contains("BarContentView") {
                        let statusBarHeight = self.hiddenStatusBar ? 0 : UIApplication.shared.statusBarFrame.height
                        subview.frame.origin.y = statusBarHeight
                        subview.frame.size.height = self.bounds.height - statusBarHeight
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题