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

Swift:代码push后largeTitleTextAttributes失效问题求助

解决思路

1. 排查ShootPVC内部的导航栏配置

  • 检查ShootPVC的viewDidLoad、viewWillAppear等生命周期方法,是否存在修改navigationController?.navigationBar.largeTitleTextAttributes或重置导航栏样式的代码,这类代码会直接覆盖外部设置的属性。
  • 确保ShootPVC的大标题显示模式正确,若设置为.never,大标题不会展示,属性设置自然无效:
    override func viewDidLoad() {
        super.viewDidLoad()
        // 确保大标题可以显示
        navigationItem.largeTitleDisplayMode = .always
        // 直接在内部统一设置大标题属性
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.firstLineHeadIndent = 10
        navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedString.Key.font: currentTheme.titleFontLarge,
            NSAttributedString.Key.foregroundColor: currentTheme.navigationBarTextColor,
            NSAttributedString.Key.paragraphStyle: paragraphStyle
        ]
    }
    

2. 调整属性设置的时机

不要在pushViewController后立刻设置属性,动画过程中系统可能会重置导航栏状态。可以利用iOS 16+提供的带完成回调的push方法:

navigationController?.pushViewController(shootSummaryVC, animated: true) { [self] in
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.firstLineHeadIndent = 10
    navigationController?.navigationBar.largeTitleTextAttributes = [
        NSAttributedString.Key.font: currentTheme.titleFontLarge,
        NSAttributedString.Key.foregroundColor: currentTheme.navigationBarTextColor,
        NSAttributedString.Key.paragraphStyle: paragraphStyle
    ]
}

低版本系统可在ShootPVC的viewDidAppear方法中设置属性,确保导航栏状态稳定后再修改。

3. 排除全局样式冲突

检查AppDelegate/SceneDelegate中的全局导航栏配置,若设置了UINavigationBar.appearance().largeTitleTextAttributes,push后系统可能会自动还原为全局样式。此时要么修改全局样式为目标格式,要么在ShootPVC内部明确覆盖全局样式。

4. 确认大标题启用状态

确保导航栏已开启大标题模式,若全局未开启,需在ShootPVC内单独设置:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.prefersLargeTitles = true
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 12:57:36