【问题标题】:Is there any way to code the LaunchScreen programmatically有没有办法以编程方式对 LaunchScreen 进行编码
【发布时间】:2015-09-29 23:48:08
【问题描述】:

我在 Storyboards 中使用 Xcode7 和 Swift。当我打开 LaunchScreen.storyboard 并尝试在其上设置自定义类时,Xcode 抱怨不能在 LaunchScreen 故事板上拥有自定义类。 所以我的问题是,有什么方法可以对 LaunchScreen 进行编程编码,因为我想避免使用 IB 在其上拖放元素。

【问题讨论】:

    标签: ios objective-c swift xcode7


    【解决方案1】:

    不,启动屏幕显示在您的应用开始执行之前,以便在加载时提供从 Springboard 到您的应用的过渡。

    您可以使用固定图像,也可以使用仅使用标准静态 UI 元素(标签和图像)的简单故事板场景。

    这个情节提要场景实际上是由操作系统加载和显示的,因此允许您的代码在该上下文中执行会带来安全风险。

    【讨论】:

    • 是的,但是在视频播放之前您仍然需要一个启动图像或启动屏幕,并且需要在 Interface Builder 中创建或者是一个简单的图像
    • 当然 - 我想这取决于从静态图像过渡到动态图像是否适用于 OP...
    • OP 的问题是避免使用 IB 来创建启动屏幕
    【解决方案2】:

    这并不能真正算作 以编程方式,因为它只是在编辑 info.plist 文件,但是根据我的研究,我发现有一种新方法正在引入,尤其是现在使用 SwiftUI 及其如何实现将替换我找到的 StoryBoards here

    我想在这里分享给那些搜索这个主题并第一次看到这篇文章的人,他们只是想要一种与使用 StoryBoards 不同的方式来创建他们的启动屏幕(比如我)。

    【讨论】:

      【解决方案3】:

      成功了

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
          // Override point for customization after application launch.
          imageArr = ["1.jpeg","2.jpeg","3.jpeg","4.jpeg"]
      
          let RandomNumber = Int(arc4random_uniform(UInt32(self.imageArr.count)))
          //imageArr is array of images
           let image = UIImage.init(named: "\(imageArr[RandomNumber])")
      
          let imageView = UIImageView.init(image: image)
          imageView.frame = UIScreen.main.bounds
      
          window = UIWindow(frame: UIScreen.main.bounds)
          window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
          window?.rootViewController?.view.addSubview(imageView)
          window?.rootViewController?.view.bringSubview(toFront: imageView)
          window?.makeKeyAndVisible()
      
          DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
              self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
          }
          return true
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-02
        • 2021-08-28
        • 1970-01-01
        相关资源
        最近更新 更多