20

I have a ViewController (B) which is handled by a PageViewController which is inside another ViewController (A) and open it modal. I have centered the ViewController (B) exactly in the middle of the screen. This works fine.

But when I push the ViewController (A) from a NavigationController the frame of ViewController (B) is to big and extends below the NavigationBar and the TabBar. But I want it centered between the NavigationBar and the TabBar.

I know how I can get the height of the navbar and the tabbar so I could resize ViewController (B):

var topBar = self.navigationController?.navigationBar.frame.height
var bottomBar = self.tabBarController?.tabBar.frame.height

But this does not work deep down in the vier hierarchy in ViewController (B). self.navigationController and self.tabBarController are nil. So how can I get the height of the navbar and tabbar deeper down in the view hierarchy? Or do I just have to pass it down from one ViewController to another till I reach ViewController (B)? Or is there another more obvious way to center the view? Thanks.

(I have tried to post screenshots for better understanding but I miss the needed reputation points to post images, sorry)

3
  • Try yourview.bounds after viewDidAppear.
    – Elto
    Jul 31, 2015 at 17:25
  • The bounds don't give me the topLayoutGuide and/or the bottonLayoutGuide
    – Darko
    Aug 31, 2017 at 13:48
  • If you have opened the view controller modally it's not a child of the controller with the Tab Bar hence self.tabBarController is nil. It sits above it in the navigation stack. You'll have to pass that info through, e.g in prepare(for segue:). If you've opened modally then why do you want the user to think they can click on the tab buttons (which they probably can't)? Maybe you don't mean to open modally.
    – RowanPD
    May 5, 2018 at 8:35

6 Answers 6

39

Use this :

let height = UIApplication.sharedApplication().statusBarFrame.height +
self.navigationController!.navigationBar.frame.height

this support portrait and landscape state.

Swift 5

let height = UIApplication.shared.statusBarFrame.height +
      self.navigationController!.navigationBar.frame.height
5
  • In this situation navigationController == nil
    – Darko
    Aug 31, 2017 at 13:47
  • 1
    Updated: UIApplication.shared.statusBarFrame.height + self.navigationController!.navigationBar.frame.height
    – spencer.sm
    Apr 14, 2018 at 3:43
  • self.navigationController!.navigationBar.frame.height is nil for me. Oct 17, 2018 at 18:23
  • May you aren't using navigationController in your controller. Oct 24, 2018 at 12:25
  • 1
    That would work ONLY if you have a UINavigationController. The OP said his UIViewController is presented modally and it's not part of a UINavigationController stack, hence this code would crash for him, since 1 - force unwrapping navigationController and 2 - he doesn't have a one in his case
    – Dani
    Mar 15, 2019 at 8:38
8

For UITabBar, this code work :

self.tabBarController?.tabBar.frame.height
1
  • In this situation the tabBarController == nil
    – Darko
    Aug 31, 2017 at 13:48
2

For Swift 3

navBarHeight = (self.navigationController?.navigationBar.intrinsicContentSize.height)! 
+ UIApplication.shared.statusBarFrame.height
1
  • In this situation navigationController == nil
    – Darko
    Aug 31, 2017 at 13:47
1

As for iOS 13, since the statusBarFrame property of a UIApplication object was deprecated, one can use:

let statusBarHeight =  navigationController?.view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0

let navigationBarHeight = navigationController?.navigationBar.frame.height ?? 0

Now, it may seem that doesn't help you @OP since your navigationController is nil, but you could try to use view.window?.windowScene?.statusBarManager?.statusBarFrame.height for the statusBar height and figure out a way to find the navBar height as well? Hope that helps :)

1

iOS 13 solution:

extension UIViewController {

    /**
     *  Height of status bar + navigation bar (if navigation bar exist)
     */

    var topbarHeight: CGFloat {

        let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first

        return (window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0) +
            (self.navigationController?.navigationBar.frame.height ?? 0.0)
    }
}

To call it you can just use: self.topbarHeight in viewController

-20

Navigation bar height status bar height and tab bar height is always constant :

  1. Navigation bar - 44pts
  2. Status bar - 20pts
  3. Tab bar - 49pts.

You can directly subtract these constants from view height: self.view.frame.size.height- (44+20+49)

6
  • Thanks, but I want to use the view sometimes modal and sometimes pushed from a NavigationController so I need at least this to know during runtime.
    – Darko D.
    May 28, 2015 at 11:03
  • 1
    This is also wrong. The status bar is 40 pts during a phone call. Aug 17, 2015 at 18:46
  • 1
    @DylanGattey This is not wrong , please read the question first , there is no reference to phone call. Aug 18, 2015 at 12:52
  • 9
    @RajdeepSingh If the user is in a phone call and is trying to use the app, the status bar is actually 40pts high, throwing off these calculations. So this answer is wrong in that situation. Your answer is not valid for all situations. Aug 18, 2015 at 17:47
  • 1
    @RajdeepSingh Also, you can never know what Apple decides to change with the next versions of iOS, even 1px difference would break this. Jun 3, 2016 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.