首页 > 解决方案 > SwiftUI NavigationBar 后退按钮在使用自定义字体时抱怨图标缩放

问题描述

运行我的应用程序时,我看到大量日志告诉我后退按钮图标 (chevron.backward) 正在缩放。

2021-10-14 13:54:32.306371+0200 MyApp[37123:18219419] [framework] CoreUI: -[CUICatalog namedVectorGlyphWithName:scaleFactor:deviceIdiom:layoutDirection:glyphSize:glyphWeight:glyphPointSize:appearanceName:] 'chevron.backward' called with scaleFactor == 2.000000 glyphPointSize == 0.000000 at '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/CoreServices/CoreGlyphs.bundle/Assets.car'
2021-10-14 13:54:32.386602+0200 MyApp[37123:18219419] [framework] CoreUI: -[CUICatalog namedVectorGlyphWithName:scaleFactor:deviceIdiom:layoutDirection:glyphSize:glyphWeight:glyphPointSize:appearanceName:] 'UINavigationBarTitleTransitionBackIndicatorMaskSymbol' called with scaleFactor == 2.000000 glyphPointSize == 0.000000 at '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/Artwork.bundle/Assets.car'

我的应用程序中的所有后退按钮都是由操作系统创建的,即我不会手动将后退按钮添加到NavigationBar. 但是,我确实NavigationBar通过设置要通过ViewModifier. 不应用视图修饰符会使所有日志静音,所以我猜我使用自定义字体会导致这些消息。该应用程序面向 iOS 15 及更高版本,我正在 XCode 13 上进行编码。

我在我的App

struct MyApp:App {
  var body: some Scene {
    WindowGroup {
      NavigationView {
        ContentView
      }
      .navigationBarThemed()
    }
  }
}

ViewModifier:_

// MARK: Navigation Bar
extension View {
  public func navigationBarThemed() -> some View {
    self.modifier(ThemedNavigationBar())
  }
}

private struct ThemedNavigationBar:ViewModifier {
  // MARK: Back button
  var backButtonAppearance: UIBarButtonItemAppearance {
    let backButtonAppearance = UIBarButtonItemAppearance()
    backButtonAppearance.normal.titleTextAttributes = [
      NSAttributedString.Key.font: UIFont(name: Font.myFont.weights.regular, size: Font.sizes.navigationBarButton)!,
      NSAttributedString.Key.foregroundColor: UIColor(Color.highlight)
    ]
    backButtonAppearance.focused.titleTextAttributes = [
      NSAttributedString.Key.font: UIFont(name: Font.myFont.weights.regular, size: Font.sizes.navigationBarButton)!,
      NSAttributedString.Key.foregroundColor: UIColor(Color.highlight)
    ]
    
    return backButtonAppearance
  }
  
  // MARK: Large Title
  var largeTitleTextAttributes:[NSAttributedString.Key : Any] = [
    NSAttributedString.Key.font: UIFont(name: Font.myFont.weights.regular, size: Font.sizes.navigationBarLargeTitle)!,
    NSAttributedString.Key.foregroundColor: UIColor(Color.text)
  ]
  
  var theme:UINavigationBarAppearance {
    let themedAppearance = UINavigationBarAppearance()
    themedAppearance.configureWithTransparentBackground()
    themedAppearance.backgroundColor = UIColor(Color.background)
    themedAppearance.backButtonAppearance = self.backButtonAppearance
    themedAppearance.largeTitleTextAttributes = self.largeTitleTextAttributes
    
    return themedAppearance
  }
  
  init() {
    UINavigationBar.appearance().standardAppearance = self.theme
    UINavigationBar.appearance().scrollEdgeAppearance = self.theme
    UINavigationBar.appearance().compactAppearance = self.theme
  }
  
  public func body(content: Content) -> some View {
    content
  }
}

如何在 NavigationBar 中使用自定义字体而不导致这些缩放日志?

标签: iosxcodeswiftuiuinavigationbar

解决方案


不是一个真正的答案,但可能对任何在这里结束的人都有帮助:

我无法解决上述主题的问题ViewModifier。相反,我创建了一个BackButton视图和一个随附的.navigationBarBackButton()视图修改器,当我想要在导航栏中返回功能时使用它们。

后退按钮:

import SwiftUI

struct BackButton: View {
  @Environment(\.dismiss) var dismiss
  
  var body: some View {
    Button(action: {
      self.dismiss()
    }) {
      HStack {
        Text(Image(systemName: "chevron.backward"))
        Text("Back")
      }
      .font(myCustomFont)
      .foregroundColor(myCustomColor)
    }
  }
}

视图修饰符:

// MARK: NavigationBar Back button
private struct NavigationBarBackButton:ViewModifier {
  public func body(content: Content) -> some View {
    content
      .navigationBarBackButtonHidden(true)
      .toolbar {
        ToolbarItem(id: "navigationBarBackButton", placement: .navigationBarLeading, showsByDefault: true) {
          BackButton()
        }
      }
  }
}

extension View {
  public func navigationBarBackButton() -> some View {
    self.modifier(NavigationBarBackButton())
  }
}

示例用法:

import SwiftUI

struct ContentView:View {
  var body: some View {
    NavigationView {
      ViewA()
        .navigationBarBackButton()
    }
  }
}

推荐阅读