Change NavigationBar Size with AdditionalSafeAreaInsets

Jun 21, 2020

Download

If you ever want something like this: Simulator Screen Shot - iPhone 11 Pro Max - 2020-06-22 at 15.04.55 Screen Shot 2563-06-22 at 15.04.04 simply add this line to your viewController

  navigationController?.additionalSafeAreaInsets.top = 100 
  // where 100 is the extra space

To see the code in action, I attached the following function to the BLUE slider

  @IBAction func sliderSlided(_ sender: Any) {
        let top: CGFloat = (CGFloat(200 * ((sender as? UISlider)?.value ?? 1) - 0.5))
        navigationController?.additionalSafeAreaInsets.top = top
  }

ezgif-3-6d10af549716

Also notice that when the NavigationBar changes, the slideIndicator changes too, indicating change in the view's size.

I also add a RED slider with the following function:

    @IBAction func slider2Slided(_ sender: Any) {
        let top: CGFloat = (CGFloat(100 * ((sender as? UISlider)?.value ?? 1)))
        additionalSafeAreaInsets.top = top
    }

ezgif-3-060fd2f92351

Notice how RED slider resizes both the content and the Navigation Bar. Moiving the BLUE slider after the RED to configure the navigationBar.

ezgif-3-6bb323653a37

AdditionalSafeAreaInsets is traditionally use to change the safeAreaInset of the ViewController.

Confusians

© 2015 - 2021