[iOS 13 UIBarButtonItem深色模式下的颜色

问题描述 投票:0回答:2

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

我通过编程方式设置了颜色,并期望在切换模式时可以在黑白之间切换。至少它与我的NavigationBar的tintColor一起使用。

我设置:

myBarButton.tintColor = UIColor.white

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

另一方面,在明亮模式下为黑色,在黑暗模式下为白色:

navigationBar.tintColor = UIColor.white

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

ios swift uibarbuttonitem uicolor ios-darkmode
2个回答
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 = ...

0
投票

UIColor.whitenot动态颜色。无论外观设置如何,它都是白色的。如果要根据外观使用不同的颜色,则需要采用新的动态系统颜色之一(例如UIColor.systemBackground在浅色模式下为白色,在暗色模式下为黑色),或创建具有不同颜色的颜色资产资产目录中明暗外观的值。

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

© www.soinside.com 2019 - 2024. All rights reserved.