SwiftUI NavigationBar自定义字体触发返回按钮图标缩放日志问题
解决方案
该日志产生的原因是你仅自定义了返回按钮的标题文本属性,没有显式配置返回箭头图标的渲染参数,系统加载默认矢量图标时获取到的字体大小为0,触发了缩放警告。你只需要在UINavigationBarAppearance的配置中显式指定返回指示器的图标即可解决问题,修改成本极低且不会影响原有主题效果。
修改你的theme变量代码如下:
var theme:UINavigationBarAppearance { let themedAppearance = UINavigationBarAppearance() themedAppearance.configureWithTransparentBackground() themedAppearance.backgroundColor = UIColor(Color.background) themedAppearance.backButtonAppearance = self.backButtonAppearance themedAppearance.largeTitleTextAttributes = self.largeTitleTextAttributes // 新增以下代码,显式配置返回箭头图标 // 可按需调整SymbolConfiguration的pointSize、weight匹配你的设计需求 let symbolConfig = UIImage.SymbolConfiguration(pointSize: 14, weight: .medium) let backIndicator = UIImage(systemName: "chevron.backward", withConfiguration: symbolConfig)? .withTintColor(UIColor(Color.highlight), renderingMode: .alwaysOriginal) themedAppearance.setBackIndicatorImage(backIndicator, transitionMaskImage: backIndicator) return themedAppearance }
补充说明
- 如果你不需要自定义返回箭头的颜色,去掉
withTintColor参数即可,只要显式加载指定名称的系统图标,就可以避免系统读取到0值的字体大小,消除日志 - 修改后返回按钮的文字样式仍然会保留你自定义的字体、字号和颜色配置,和原有主题效果完全一致
- 该方案兼容iOS 15+的系统版本要求
内容的提问来源于stack exchange,提问作者Haensl
相关产品推荐
相关产品推荐

