0

I have a UIViewController that is part of my UITabBarController and I have a function that hides the tab bar to bring up a view at the bottom very similar to the picture below.

Basically whenever I get the safe area after hiding the tabBar, it doesn't adjust to the tab bar being hidden. I want to get the actual safe area if there wasn't a tab bar there so it will look the same across devices.

Is it possible to get the the root safe area insets without tab bar/navigation?

Nick
  • 247
  • 2
  • 9
  • it is hard to imaging what you are doing in the code did you present the new view controller? – Supran Jowti Jun 05 '20 at 04:33
  • I should have been a little more clear there, it isn't a new view controller but a UIView in the view controller that pops up and hides the tab bar simultaneously – Nick Jun 05 '20 at 04:36

3 Answers3

2

You can present in tab bar which will present on top of it so no need to hide the tab bar.

tabBarController?.present(yourViewController, animated: true, completion: nil)
Val Moratalla
  • 191
  • 1
  • 9
2

If you use AutoLayout and want to keep your custom tab bar at the bottom of the screen while taking into account the safe area on iPhone X for example, you can simply use view.safeAreaLayoutGuide.bottomAnchor to pin your custom view to the bottom of the screen.

For example:

let myCustomTabBar = UIView()
// ...
view.addSubview(myCustomTabBar)
myCustomTabBar.translatesAutoresizingMaskIntoConstraints = false
myCustomTabBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
myCustomTabBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myCustomTabBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
myCustomTabBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

If you want to know the safe area insets for your window (without taking into account your tab bar or navigation bar) you can access the UIWindow's safeAreaInsets property, or check out its safeAreaLayoutGuide.layoutFrame.

You can also override the viewSafeAreaInsetsDidChange of your UIViewController and make the needed adjustments depending on what behaviour you're looking for.

1
guard let rootView = UIApplication.shared.keyWindow else { return 0 }

        if #available(iOS 11.0, *) {

//            let topInset = rootView.safeAreaInsets.top

            let bottomInset = rootView.safeAreaInsets.bottom



        } else {

            return rootView.bounds.height

        }

https://stackoverflow.com/a/47831485/2815002

Supran Jowti
  • 371
  • 2
  • 9