Ios 使用SwiftUI的UINavigationBar的自定义背景色

Ios 使用SwiftUI的UINavigationBar的自定义背景色,ios,swift,swiftui,Ios,Swift,Swiftui,我正在尝试使用SwiftUI为UINavigationBar使用我自己的背景色,但无法使用以下代码对其进行更改: let attrs = [ NSAttributedString.Key.foregroundColor: UIColor.blue, NSAttributedString.Key.font: UIFont(name: "AbrilFatface-Regular", size: 24)!] UINavigationBar.appearance().titleTe

我正在尝试使用SwiftUI为UINavigationBar使用我自己的背景色,但无法使用以下代码对其进行更改:

let attrs = [
     NSAttributedString.Key.foregroundColor: UIColor.blue,
     NSAttributedString.Key.font: UIFont(name: "AbrilFatface-Regular", size: 24)!]

UINavigationBar.appearance().titleTextAttributes = attrs
UINavigationBar.appearance().backgroundColor = UIColor(red:253/255, green:213/255, blue:211/255, alpha:1.0)

您的代码与SwiftUI无关。要更改UINavigationBar外观,必须使用UIKit,它是SwiftUI的前身。下面的代码将全局更改导航栏

//实例化新的UINavigationBarAppearance对象
让navigationBarAppearance=UINavigationBarAppearance()
//设置背景色
navigationBarAppearance.backgroundColor=.black
//设置标题的文本颜色
navigationBarAppearance.titleTextAttributes=[.foregroundColor:UIColor.white]
//设置大标题的文本颜色
navigationBarAppearance.LarGetTitletExtAttributes=[.foregroundColor:UIColor.white]
//指定外观
UINavigationBar.appearance().standardAppearance=navigationBarAppearance
UINavigationBar.appearance().compactAppearance=navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance=navigationBarAppearance
AppDelegate.swift文件中调用此函数,在应用程序中(\uUdFinishLaunchingWithOptions:)方法。此代码适用于iOS 13+


这是苹果的文档,以备不时之需。

请接受此答案,以便我们知道这是解决方案。你可以在我的问题旁边打勾接受