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

iOS15下如何将UIActivityViewController分享弹窗导航栏设为不透明

iOS 15 UIActivityViewController 导航栏透明问题修复方案

根因

iOS 15 开始 UIActivityViewController 内部的导航控制器由系统独立管理,全局导航栏配置不会自动继承,且你在 present 之前访问 activityVC.navigationController 会返回空值,之前的配置操作没有实际生效,才会出现部分场景透明的问题。不同分享内容对应的系统内部页面结构不同,是你部分场景正常部分异常的原因。

修复代码

全局一次性修复(推荐)

直接在 AppDelegate 的全局导航栏配置逻辑中,新增针对 UIActivityViewController 的专属外观配置:

// AppDelegate.swift 原有全局配置外补充以下代码
if #available(iOS 15.0, *) {
    let shareNavAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [UIActivityViewController.self])
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .white
    appearance.shadowImage = nil
    appearance.shadowColor = .clear
    appearance.titleTextAttributes = [
        NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
        NSAttributedString.Key.foregroundColor: UIColor.Function.black
    ]
    shareNavAppearance.standardAppearance = appearance
    shareNavAppearance.scrollEdgeAppearance = appearance
    shareNavAppearance.compactAppearance = appearance
    shareNavAppearance.isTranslucent = false
}

单场景临时修复

如果只需要在部分分享场景生效,可以在弹出分享框的方法中加以下逻辑:

func presentShareActivity(items: [Any]) {
    let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
    present(activityVC, animated: true) {
        if #available(iOS 13.0, *) {
            guard let navBar = activityVC.navigationController?.navigationBar else { return }
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .white
            appearance.shadowImage = nil
            appearance.shadowColor = .clear
            appearance.titleTextAttributes = [
                NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
                NSAttributedString.Key.foregroundColor: UIColor.Function.black
            ]
            navBar.standardAppearance = appearance
            navBar.scrollEdgeAppearance = appearance
            navBar.compactAppearance = appearance
            navBar.isTranslucent = false
        }
    }
}

这个方案是等分享弹窗弹出完成后,再拿到实际存在的导航栏对象修改配置,避免空对象操作无效的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 15:24:02