代码之家  ›  专栏  ›  技术社区  ›  Adolfo Nicoloso

使用Swift更改App Navigator上“后退”按钮的颜色和符号

  •  2
  • Adolfo Nicoloso  · 技术社区  · 7 月前

    我试图实现所附的返回导航外观:

    Desired Design for 'Back' Button

    我也想保留后滑功能。

    从一年前苹果论坛的一个问题来看,我想做的是“不受官方支持”。尽管如此,我还是想在Stack Overflow上提问。
    我需要明确区分实际符号和按钮文本。

    到目前为止,我发现了三件事:

    1. 您可以通过以下操作隐藏标准导航栏和阴影:
    // Set up the UINavigationBarAppearance
    let appearance = UINavigationBarAppearance()
    appearance.configureWithTransparentBackground() // Transparent background
    appearance.shadowColor = nil // Remove shadow
    appearance.backgroundColor = .clear // Clear background
    
    // Apply appearance globally
    UINavigationBar.appearance().standardAppearance = appearance
    
    1. 您可以通过以下操作将按钮的标准导航“V形向后”符号更改为另一个苹果提供的符号:
    // Set up the UINavigationBarAppearance
    let appearance = UINavigationBarAppearance()
    
    // Create a static black version of the back button image
    let backImage = UIImage(systemName: [insert symbol name here])
    appearance.setBackIndicatorImage(backImage, transitionMaskImage: backImage)
            
    // Apply appearance globally
    UINavigationBar.appearance().standardAppearance = appearance
    
    1. 您可以通过以下操作自定义标准导航的“返回”文本:
    // Customize back button appearance
    let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
    backButtonAppearance.normal.titleTextAttributes = [
        .font: UIFont.systemFont(ofSize: 16, weight: .medium),
        .foregroundColor: UIColor.black // Text color
    ]
    appearance.backButtonAppearance = backButtonAppearance
            
    // Apply appearance globally
    UINavigationBar.appearance().standardAppearance = appearance
    

    然后,在一个视图中,它看起来像这样(对我来说):

    struct StartupPage2: View {
        init() {
            // Set up the UINavigationBarAppearance
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground() // Transparent background
            appearance.shadowColor = nil // Remove shadow
            appearance.backgroundColor = .clear // Clear background
    
            // Customize back button appearance
            let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
            backButtonAppearance.normal.titleTextAttributes = [
                .font: UIFont.systemFont(ofSize: 16, weight: .medium), // Text font
                .foregroundColor: UIColor.black // Text color
            ]
            appearance.backButtonAppearance = backButtonAppearance
    
            // Create a static black version of the back button image
            let backImage = UIImage(systemName: "chevron.backward") // Change UIImage 
            appearance.setBackIndicatorImage(backImage, transitionMaskImage: backImage)
                
            // Apply appearance globally
            UINavigationBar.appearance().standardAppearance = appearance
        }
    

    但是,我找不到一种方法来改变V形的颜色,使其看起来像我的设计附件。

    1 回复  |  直到 7 月前
        1
  •  1
  •   sonle    7 月前

    我想你失踪了 tintColor 对于背面指示器图像,请尝试以下操作:

    let backImage = UIImage(systemName: "chevron.backward")?
        .withTintColor(.purple, renderingMode: .alwaysOriginal) //<- put the desired color here
    
    appearance.setBackIndicatorImage(backImage, transitionMaskImage: backImage)