SwiftUI 2.onChange()中的选择器不会更改UINavigationBar.appearance()

SwiftUI 2.onChange()中的选择器不会更改UINavigationBar.appearance(),swift,xcode,swiftui,onchange,picker,Swift,Xcode,Swiftui,Onchange,Picker,这是一个奇怪的问题……在SwiftUI 2启动之前,我在视图的init()中设置了UINavigationBar.appearance(),如下所示: init(selectedStyle: Binding<Int>) { _selectedStyle = selectedStyle if self.selectedStyle == 1 { UINavigationBar.appearance().backgroundColor

这是一个奇怪的问题……在SwiftUI 2启动之前,我在视图的init()中设置了UINavigationBar.appearance(),如下所示:

init(selectedStyle: Binding<Int>) {
        _selectedStyle = selectedStyle
        if self.selectedStyle == 1 {
            UINavigationBar.appearance().backgroundColor = UIColor.init(displayP3Red: 7/255, green: 7/255, blue: 7/255, alpha: 1)
            UISegmentedControl.appearance().backgroundColor = .black
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.selected)
            UISegmentedControl.appearance().selectedSegmentTintColor = .white
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: UIControl.State.normal)
        } else {
            UINavigationBar.appearance().backgroundColor = .white
            UISegmentedControl.appearance().backgroundColor = .white
            UISegmentedControl.appearance().selectedSegmentTintColor = .white
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.selected)
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
        }
    }
init(selectedStyle:Binding){
_selectedStyle=selectedStyle
如果self.selectedStyle==1{
UINavigationBar.appearance().backgroundColor=UIColor.init(displayP3Red:7/255,green:7/255,blue:7/255,alpha:1)
UISegmentedControl.appearance().backgroundColor=.black
UINavigationBar.appearance().titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.white]
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black],用于:UIControl.State.selected)
UISegmentedControl.appearance().selectedSegmentTintColor=.white
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.white],用于:UIControl.State.normal)
}否则{
UINavigationBar.appearance().backgroundColor=.white
UISegmentedControl.appearance().backgroundColor=.white
UISegmentedControl.appearance().selectedSegmentTintColor=.white
UINavigationBar.appearance().titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.black]
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black],用于:UIControl.State.selected)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black],用于:UIControl.State.normal)
}
}
在Swiftui1中,视图将再次初始化,以便导航栏的新外观将正确更新。因为init()函数将再次运行。我试图将相同的代码放入附加到选择器的onChange()中,但由于某些原因,它无法工作:

Picker(selection: $selectedStyle, label: Text("")) {
    ForEach(0 ..< 3) {
        Text([$0])
    }
}
.pickerStyle(SegmentedPickerStyle())
.onChange(of: selectedStyle, perform: { change in
    if self.selectedStyle == 1 {
            UINavigationBar.appearance().backgroundColor = UIColor.init(displayP3Red: 7/255, green: 7/255, blue: 7/255, alpha: 1)
            UISegmentedControl.appearance().backgroundColor = .black
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.selected)
            UISegmentedControl.appearance().selectedSegmentTintColor = .white
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: UIControl.State.normal)
        } else {
            UINavigationBar.appearance().backgroundColor = .white
            UISegmentedControl.appearance().backgroundColor = .white
            UISegmentedControl.appearance().selectedSegmentTintColor = .white
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.selected)
            UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
        }
    })
Picker(选择:$selectedStyle,标签:Text(“”)){
ForEach(0..<3){
文本([$0])
}
}
.pickerStyle(SegmentedPickerStyle())
.onChange(of:selectedStyle,执行:{Changein
如果self.selectedStyle==1{
UINavigationBar.appearance().backgroundColor=UIColor.init(displayP3Red:7/255,green:7/255,blue:7/255,alpha:1)
UISegmentedControl.appearance().backgroundColor=.black
UINavigationBar.appearance().titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.white]
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black],用于:UIControl.State.selected)
UISegmentedControl.appearance().selectedSegmentTintColor=.white
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.white],用于:UIControl.State.normal)
}否则{
UINavigationBar.appearance().backgroundColor=.white
UISegmentedControl.appearance().backgroundColor=.white
UISegmentedControl.appearance().selectedSegmentTintColor=.white
UINavigationBar.appearance().titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.black]
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black],用于:UIControl.State.selected)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black],用于:UIControl.State.normal)
}
})

设置相应外观后,
外观对创建的UI元素有影响。因此,您需要重新创建所有依赖的UI

可能的方法如下-假设您在
ContentView
的根目录中有
NavigationView
,您可以通过

var主体:一些视图{
导航视图{
//…满足于此

}.id(selectedStyle)//但是我实际上如何重新创建它?另外,我要澄清的是,.id属性只是提供了对特定代码块的引用,对吗?是的,但是当重新创建id更改视图时,因为它应该已经是不同的视图了。
var body: some View {
   NavigationView {

     // .. content here

   }.id(selectedStyle)       // << here !!
}