为什么Xcode 13.1中设置Bar Tint属性颜色不生效?
原因说明
Xcode 13及后续版本创建的项目默认适配iOS 15+的新导航栏外观规则,旧版的barTint属性被新的UINavigationBarAppearance相关配置覆盖,所以直接在Attributes Inspector中修改Bar Tint不会生效。
解决方案
方案1:仅通过Storyboard配置(无需编写代码)
- 选中导航控制器对应的导航栏组件,打开右侧的Attributes Inspector面板
- 找到Appearance配置区,取消
Standard和Scroll Edge两个选项的勾选 - 重新设置
Bar Tint属性为system blue,即可在Interface Builder中实时看到修改效果
方案2:通过代码全局统一配置
如果需要整个App所有导航栏都使用该样式,可以在AppDelegate的application:didFinishLaunchingWithOptions:方法中添加对应代码:
Swift 示例
let appearance = UINavigationBarAppearance() appearance.configureWithOpaqueBackground() appearance.backgroundColor = .systemBlue // 同时配置两种滚动状态下的外观,保证所有场景样式一致 UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().scrollEdgeAppearance = appearance
Objective-C 示例
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init]; [appearance configureWithOpaqueBackground]; appearance.backgroundColor = [UIColor systemBlueColor]; [UINavigationBar appearance].standardAppearance = appearance; [UINavigationBar appearance].scrollEdgeAppearance = appearance;
提示:iOS 15开始导航栏默认会根据页面滚动位置切换外观,
scrollEdgeAppearance对应页面滚动到顶部时的导航栏样式,standardAppearance对应页面有滚动偏移时的样式,两者都配置才能避免滚动时出现颜色突变。
内容的提问来源于stack exchange,提问作者sjlearmonth
相关产品推荐
相关产品推荐

