在 iOS 13 中,他们改变了导航栏颜色的操作方式。现在他们使用 UINavigationBarAppearance 和 UIBarButtonItemAppearance 来自定义导航栏,以及 standardAppearance 和 scrollEdgeAppearance。
我正在寻找一种方法来为 standardAppearance 和 scrollEdgeAppearance 使用不同的导航栏色调。或者能够为每个外观更改条形按钮图标颜色。
//set the standard nav bar appearance
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = UIColor.mainAppColorForNavBar
//set bar button appearance
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.white]
navBarAppearance.buttonAppearance = buttonAppearance
UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self]).standardAppearance = navBarAppearance
//set the scroll edge nav bar appearance
let scrollNavBarAppearance = UINavigationBarAppearance()
scrollNavBarAppearance.configureWithOpaqueBackground()
scrollNavBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.label]
scrollNavBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.label]
//set bar button appearance
let scrollButtonAppearance = UIBarButtonItemAppearance()
scrollButtonAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.label]
scrollNavBarAppearance.buttonAppearance = scrollButtonAppearance
UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self]).scrollEdgeAppearance = scrollNavBarAppearance
这将设置导航栏色调颜色但不区分 standardAppearance 和 scrollEdgeAppearance。
UINavigationBar.appearance().tintColor = UIColor.white
目前在 scrollEdgeAppearance 中(看起来是我想要的方式,不需要更改)
目前在 standardAppearance (按钮丢失,因为它与背景颜色相同,我想在 standardAppearance 中将图标颜色更改为白色)
感谢任何帮助。
谢谢,
最佳答案
我在后退按钮上遇到了类似的问题,并通过为每个外观设置具有不同渲染模式的图像来解决它。这避免了应用 tintColor。
// demo image
let image = UIImage(systemName: "ellipsis.circle")!
// image with .alwaysOriginal rendering mode to avoid tintColor application
let imageForNavBarAppearance = image
.withTintColor(.black)
.withRenderingMode(.alwaysOriginal)
let imageForScrollNavBarAppearance = image
.withTintColor(.green)
.withRenderingMode(.alwaysOriginal)
// your UINavigationBarAppearance instances
navBarAppearance.setBackIndicatorImage(imageForNavBarAppearance, transitionMaskImage: imageForNavBarAppearance)
scrollNavBarAppearance.setBackIndicatorImage(imageForScrollNavBarAppearance, transitionMaskImage: imageForScrollNavBarAppearance)
这只解决了后退按钮的问题。
还有两个其他选项可以为栏项外观设置背景图像。
UINavigationBarAppearance.buttonAppearance
具有背景图像的外观配置将应用于所有栏项。
这应该没问题,因为您只有一个栏项。
UINavigationBarAppearance.doneButtonAppearance
如果您要为右上角的“+”符号创建一个完成样式的栏项,则应该应用此外观配置。
let ap = UIBarButtonItemAppearance()
ap.normal.backgroundImage = image.withTintColor(.black).withRenderingMode(.alwaysOriginal)
let scrollAp = UIBarButtonItemAppearance()
scrollAp.normal.backgroundImage = image.withTintColor(.green).withRenderingMode(.alwaysOriginal)
// apply to all bar items
navBarAppearance.buttonAppearance = ap
scrollNavBarAppearance.buttonAppearance = scrollAp
// or apply to done buttons
navBarAppearance.doneButtonAppearance = ap
scrollNavBarAppearance.doneButtonAppearance = scrollAp
关于ios - iOS 13 中的条形按钮色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940448/