简体   繁体   English

SwiftUI - 带有 NavigationView 的 TabView 生成灰色区域

[英]SwiftUI - TabView with NavigationView generates gray area

I have some problems with my tabbed view when I set isTranslucent to false in combination with a NavigationView .当我将isTranslucent设置为false并结合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.我需要将半透明设置为 false 否则我无法获得深色。

在此处输入图像描述

You can set backgroundColor.您可以设置背景颜色。 Don't set isTranslucent to false or it will create these artefacts you mentioned.不要将 isTranslucent 设置为 false ,否则它会创建您提到的这些人工制品。

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:编辑:刚刚观看了 iOS 13 的 UI 现代化改造 这是这样做的方法:

The TabView and NavigationView are actually UIHostedController for the legacy UITabBarController and UINavigationController: TabView 和 NavigationView 实际上是旧版 UITabBarController 和 UINavigationController 的 UIHostedController:

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.应该有一种更简洁的方式来访问 tabBar 和 navBar。

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

I was using UIKit with SwiftUI.我正在使用 UIKit 和 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.我的标签栏是在 storyboard 中创建的,但是我获得额外底部空间的视图是 swiftui 视图,正如您提到的那样。 I tried all above solution but Nothing worked for me.我尝试了上述所有解决方案,但对我没有任何帮助。

I am using Xcode 12.4 for development.我正在使用 Xcode 12.4 进行开发。 My solution is to mark Translucent to true in storyboard and Bottom extra gray bar was gone.我的解决方案是在 storyboard 中将 Translucent 标记为 true,并且底部额外的灰色条消失了。

看截图

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() .请注意,覆盖的 function 必须是viewDidLoad() At least it doesn't work for me when it is a viewDidAppear(:) function.至少当它是viewDidAppear(:) function 时它对我不起作用。

You are changing the translucency on a UITabBar and expecting it to work on TabBar.您正在更改 UITabBar 上的半透明度并期望它在 TabBar 上工作。 The two things are not interchangeable.这两件事不可互换。

It's easier than all that, just delete the next line:这比这更容易,只需删除下一行:

UITabBar.appearance().isTranslucent = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM