深色模式下的 iOS 13 UIBarButtonItem 颜色

标签 ios swift uibarbuttonitem uicolor ios-darkmode

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

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

我设置:

myBarButton.tintColor = UIColor.white

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

另一方面,下面是浅色模式下的黑色和深色模式下的白色:

navigationBar.tintColor = UIColor.white

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

最佳答案

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

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

关于深色模式下的 iOS 13 UIBarButtonItem 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58341337/

相关文章:

ios - 将 SKScene 添加到 UIViewController?

ios - 动态改变UIImageview的高度

ios - 从应用商店更新后应用崩溃。问题可能与核心数据有关?

swift - P384 公钥获取 "IncorrectParameterSize"

ios - 透明 查看颜色从黑色到其他颜色?

ios - 从 subview Controller 中隐藏父 View 的 UIView

objective-c - 如何创建一个 UIButton 以显示与 UIBarButtonItem 相同的效果?

iphone - 使用 UIAppearance 时 UIBarButtonItem 的奇怪行为

ios - 使用google帐户登录后如何执行segue?

objective-c - 我怎样才能拥有一个同时包含图像和文本的 UIBarButtonItem?