You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不启用兼容标志的情况下修改iOS 26中UINavigationBar按钮样式?

解决iOS 26导航栏按钮大圆角样式问题

问题说明

更新Xcode并针对iOS 26构建应用后,UINavigationBar上的按钮默认变为大圆角矩形外观,需在不启用UIDesignRequiresCompatibility兼容标志的前提下,恢复旧版按钮样式,同时保留iOS 26的导航栏透明效果。

样式对比

  • 旧版外观(兼容标志开启):
    旧版导航栏按钮样式
  • iOS 26新版外观(兼容标志关闭):
    iOS 26新版导航栏按钮样式

解决方案

1. 全局统一配置(推荐)

在App启动阶段(如AppDelegate的didFinishLaunchingWithOptions或SceneDelegate的scene(_:willConnectTo:options:))添加代码,通过UIAppearance统一设置所有导航栏按钮的旧版样式:

// 配置按钮文字属性,匹配旧版字体
UIBarButtonItem.appearance().setTitleTextAttributes([
    .font: UIFont.systemFont(ofSize: 17)
], for: .normal)

// 自定义按钮背景样式
let barButtonAppearance = UIBarButtonAppearance()
barButtonAppearance.configureWithDefaultBackground()
barButtonAppearance.background.cornerRadius = 4 // 旧版按钮圆角一般为4-6,可按需微调
barButtonAppearance.backgroundColor = .clear // 保留透明效果
barButtonAppearance.normal.titleTextAttributes = [
    .foregroundColor: UIColor.systemBlue
]

// 将样式应用到导航栏
UINavigationBar.appearance().standardAppearance.buttonAppearance = barButtonAppearance
UINavigationBar.appearance().scrollEdgeAppearance = barButtonAppearance

2. 单个按钮自定义配置

若仅需修改特定页面的导航栏按钮,直接为目标UIBarButtonItem设置自定义外观:

let customAppearance = UIBarButtonAppearance()
customAppearance.configureWithDefaultBackground()
customAppearance.background.cornerRadius = 4
customAppearance.backgroundColor = .clear

let customButton = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(backAction))
customButton.standardAppearance = customAppearance
navigationItem.leftBarButtonItem = customButton

补充:系统返回按钮适配

若需修改系统自带的返回按钮样式,需额外配置:

let backButtonAppearance = UIBarButtonAppearance()
backButtonAppearance.configureWithDefaultBackground()
backButtonAppearance.background.cornerRadius = 4
UINavigationBar.appearance().standardAppearance.backButtonAppearance = backButtonAppearance

内容的提问来源于stack exchange,提问作者Ant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 08:52:37