5

I have some problems with my tabbed view when I set isTranslucent to false in combination with a NavigationView.

Does anyone know how to fix this? The problem is shown in the attached image.

I need translucent set to false otherwise I can't get the dark color.

enter image description here

Sean
  • 2,106
  • 2
  • 16
  • 24
Dyngberg
  • 663
  • 1
  • 7
  • 10

4 Answers4

7

You can set backgroundColor. Don't set isTranslucent to false or it will create these artefacts you mentioned.

UITabBar.appearance().backgroundColor = .black
UINavigationBar.appearance().backgroundColor = .black

It becomes much darker. It isn't completely opaque though.

Edit: Just watched Modernizing Your UI for iOS 13 This is the way to do it :

The TabView and NavigationView are actually UIHostedController for the legacy UITabBarController and UINavigationController:

let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor  .white]

Then set the appearance on the various type of appearance.

tabBar.standardAppearance = appearance

2nd Edit:

extension UINavigationController {
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        navigationBar.standardAppearance = appearance
        navigationBar.compactAppearance = appearance
        navigationBar.scrollEdgeAppearance = appearance
    }
}

extension UITabBarController {
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        tabBar.standardAppearance = appearance
    }
}

There should be a cleaner way to get to both tabBar and navBar.

Reference: https://developer.apple.com/videos/play/wwdc2019/224/

Kugutsumen
  • 878
  • 8
  • 18
1

I was using UIKit with SwiftUI. My Tab bar was creating in storyboard but the view for which I was getting extra bottom space was a swiftui view as you mentioned. I tried all above solution but Nothing worked for me.

I am using Xcode 12.4 for development. My solution is to mark Translucent to true in storyboard and Bottom extra gray bar was gone.

See screenshot

Mohit Kumar
  • 669
  • 5
  • 8
0

Just customize it in an extension like this:

extension UITabBarController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UITabBarAppearance()
        appearance.backgroundColor = .black
        tabBar.standardAppearance = appearance
    }
}

Pay attention that the overridden function must be viewDidLoad(). At least it doesn't work for me when it is a viewDidAppear(:) function.

Nikaaner
  • 1,022
  • 16
  • 19
-2

It's easier than all that, just delete the next line:

UITabBar.appearance().isTranslucent = false
jherediagu
  • 182
  • 1
  • 5