当系统主题发生变化时,如何改变工具栏的颜色?



我设置了UIToolBar.appearance()onappearance中的tintColor。但是,当默认主题更改为深色或浅色时,我无法更改底部栏的颜色。我的问题是我如何能够改变它,这是我的代码:

.onAppear {
if colorScheme == .light {
UIToolbar.appearance().barTintColor = UIColor(self.backgroundColorBottom)
} else {
toolBackground = UIColor(red: 38/255, green: 40/255, blue: 42/255, alpha: 100/100)
}
}
.onChange(of: colorScheme, perform: { _ in
if colorScheme == .dark {
UIToolbar.appearance().barTintColor = UIColor(red: 38/255, green: 40/255, blue: 42/255, alpha: 1/100)
} else {
UIToolbar.appearance().barTintColor = UIColor(self.backgroundColorBottom)
}
})

所以,如果我运行应用程序-颜色是正确的。但是,当我切换到背景并改变主题时。底部用户工具栏的颜色没有改变。有什么办法可以改变吗?

我让UIToolBar完全透明。它的颜色是视图后面的颜色

我创建了一个扩展到UIToolBar

extension UIToolbar {
func setBackgroundColor(image: UIImage) {
setBackgroundImage(image, forToolbarPosition: .any, barMetrics: .default)
}
}

,然后创建一个纯色的图像。

.onAppear {
UIToolbar.appearance().setBackgroundColor(image: UIImage(color: .clear, size: CGSize(width: UIScreen.main.bounds.width, height: 34))!)
}

最新更新