use*_*205 8 uinavigationcontroller ios swift ios13
在 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
Run Code Online (Sandbox Code Playgroud)
这将设置导航栏色调颜色,但不区分 standardAppearance 和 scrollEdgeAppearance。
UINavigationBar.appearance().tintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)
当前处于 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)
Run Code Online (Sandbox Code Playgroud)
这仅解决了后退按钮的问题。
还有另外两个选项可以为栏项目外观设置背景图像。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1907 次 |
| 最近记录: |