ios - iOS 13 中的条形按钮色调颜色

标签 ios swift uinavigationcontroller 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

这将设置导航栏色调颜色但不区分 standardAppearance 和 scrollEdgeAppearance。

UINavigationBar.appearance().tintColor = UIColor.white

目前在 scrollEdgeAppearance 中(看起来是我想要的方式,不需要更改) enter image description here

目前在 standardAppearance (按钮丢失,因为它与背景颜色相同,我想在 standardAppearance 中将图标颜色更改为白色) enter image description here

感谢任何帮助。

谢谢,

最佳答案

我在后退按钮上遇到了类似的问题,并通过为每个外观设置具有不同渲染模式的图像来解决它。这避免了应用 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/

相关文章:

ios - 如何为某些请求提供远程文件(从远程 SMB 服务器获取)

ios - 导航控制嵌入在容器 View 自定义 segue 中

iOS 接入点 : sending the device token to the provider in string format

ios - Swift 中的嵌套间接枚举

ios - 无法转换类型为 'UINavigationController' 错误的值

ios - observeValueForKeyPath 在 swift 中崩溃应用程序

swift - Swift 可选类型中 "an Optional of an Optional"的用途是什么?

ios - 如何下载并保存音频文件,然后快速播放?

ios - 如何以模态方式呈现 VC,然后转至嵌入导航 Controller 中的 VC

iphone - 如何显示不属于 UINavigationController 根目录的 UITabBar?