0

在深色和浅色模式之间切换时,我的 UIBarButtonItem 图像不会改变其颜色。

我以编程方式设置颜色,并希望在切换模式时它会在黑白之间变化。至少它适用于我的 NavigationBar 的 tintColor。

我设置:

myBarButton.tintColor = UIColor.white

并且按钮的图像在黑暗和明亮模式下保持白色。

另一方面,以下是亮模式下的黑色和暗模式下的白色:

navigationBar.tintColor = UIColor.white

为什么它的行为有所不同,我如何将此功能添加到我的 UIBarButtonItem?

4

2 回答 2

1

UIColor.white不是动态颜色。无论外观设置如何,它都是白色的。如果您想要一种因外观而异的颜色,您需要采用一种新的动态系统颜色(例如,UIColor.systemBackground在浅色模式下为白色,在深色模式下为黑色),或创建具有不同颜色值的颜色资源和资产目录中的深色外观。

以下是有关新系统颜色的更多信息:https ://developer.apple.com/design/human-interface-guidelines/ios/visual-design/color#dynamic-system-colors

于 2019-10-11T14:53:22.093 回答
0

使用 iOS 13 的新外观 API: https ://developer.apple.com/documentation/uikit/uinavigationbarappearance

例子:

let navStyle = UINavigationBarAppearance()

let buttonStyle = UIBarButtonItemAppearance()
// Change properties of buttonStyle here with dynamic colours such as UIColor.label.
style.buttonAppearance = buttonStyle
style.doneButtonAppearance = ...
style.backButtonAppearance = ...

navigationController?.navigationBar.standardAppearance = navStyle
navigationController?.navigationBar.scrollEdgeAppearance = ...
navigationController?.navigationBar.compactAppearance = ...
于 2019-10-11T12:47:40.850 回答