Search code examples
swiftuikituitabbarcontrollernslayoutconstraint

Safe area not working in custom TabBarController's ViewController


In the didFinishLaunchingWithOptions of my app's AppDelegate, after initializing my UIWindow property, I set the root controller to a subclass of UITabBarController, in which I set the view controllers (and some other custom behaviour).

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = TabBarController() // a subclass of UITabBarController
    self.window?.makeKeyAndVisible()

    return true
}

The issue I have is that the safe areas in the TabBarController's view controllers does not seem to work properly.

eg: sticking a view to the bottom of a view controller's view, using it's safeAreaLayoutGuide, goes behind the tab bar.

self.bottomView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)

The custom behaviour (such as set tint colors, set the viewControllers and their tab bar items, or set the tabBarController's delegate - nothing crazy here) setup is made in it's viewDidLoad.

NB: I work in code only, I do not use interface builder.


Solution

  • The issue was the way I presented programmatically the tab bar controller. What I did in the AppDelegate's didFinishLaunchingWithOptions was :

        self.window?.rootViewController = MyTabBarController()
        self.window?.makeKeyAndVisible()
    

    I fixed the issue buy putting the tabbar in in a NavigationController :

        let navigationController = UINavigationController(rootViewController: MyTabBarController())
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()