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

Swift开发:如何动态修改LaunchScreen中的UILabel文本?

实现动态启动屏文字的合规方案

Apple确实禁止在启动过程中动态修改LaunchScreen的内容(因为LaunchScreen是系统在App启动前静态渲染的,此时App代码还未执行),市面上的同类效果都是通过App启动后模拟LaunchScreen的过渡界面实现的,以下是两种合规且可落地的方案:

方案1:过渡页模拟启动屏(最常用)

核心思路是制作一个和LaunchScreen完全一致的ViewController,在App启动后先显示这个过渡页,根据首次启动状态设置对应文字,再切换到主界面。

步骤

  1. 创建SplashViewController,布局完全复刻LaunchScreen(背景色、文字位置/字体等保持一致),添加一个可动态设置文字的UILabel。
  2. 用UserDefaults记录首次启动状态,在App启动入口(SceneDelegate或AppDelegate)判断状态并设置过渡页文字。
  3. 显示过渡页一段时间后,切换到主界面。

代码示例

SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = UIWindow(windowScene: windowScene)
    self.window = window
    
    // 判断首次启动状态
    let hasLaunched = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
    let splashVC = SplashViewController()
    splashVC.displayText = hasLaunched ? "hello" : "welcome"
    
    // 标记首次启动完成
    if !hasLaunched {
        UserDefaults.standard.set(true, forKey: "hasLaunchedBefore")
    }
    
    window.rootViewController = splashVC
    window.makeKeyAndVisible()
    
    // 延迟切换到主界面(保持和系统启动屏相近的停留时间)
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
        let mainVC = YourMainViewController() // 替换为你的主界面控制器
        window.rootViewController = mainVC
    }
}

SplashViewController.swift

class SplashViewController: UIViewController {
    var displayText: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 完全复刻LaunchScreen的布局
        view.backgroundColor = .white
        
        let label = UILabel()
        label.text = displayText
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 24, weight: .medium)
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
}

方案2:无感知切换RootVC(更贴近原生体验)

核心思路是利用LaunchScreen的短暂停留时间,在App启动后立刻替换RootVC为复刻的启动界面,用户几乎察觉不到切换:

  1. 保持LaunchScreen的基础布局(比如空的文字占位)。
  2. App启动后直接设置复刻的启动界面为RootVC,同时设置对应文字。
  3. 无需额外延迟,等系统自动结束LaunchScreen停留后,用户看到的就是带目标文字的界面,随后可直接切换到主界面。

这种方案的代码逻辑和方案1基本一致,只是可以去掉延迟切换的逻辑,让主界面加载完成后直接替换即可。

关键注意事项

  • 绝对不要尝试动态修改LaunchScreen的storyboard/xib文件,或在启动过程中通过代码操作LaunchScreen,这会违反Apple审核规则导致被拒。
  • 过渡页的布局必须和LaunchScreen完全一致,避免出现视觉跳变影响体验。
  • 不要让过渡页停留超过2秒,保持和系统启动屏一致的节奏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 21:22:36