21

I am using Xcode7 and Swift with Storyboards. When I open the LaunchScreen.storyboard, and I try to set a Custom Class on it, Xcode complains that one cannot have a custom class on a LaunchScreen storyboard. So my question is, is there any way to programmatically code the LaunchScreen as I would like to avoid having to drag and drop elements on it using IB.

Arunabh Das
  • 13,212
  • 21
  • 86
  • 109

3 Answers3

43

No, The launch screen is shown before your app starts executing in order to provide a transition from the Springboard to your app while it is loading.

You can either use a fixed image or you can use a simple storyboard scene using only standard, static UI elements - labels & images.

This storyboard scene is actually loaded and displayed by the OS, so it would be a security risk to allow your code to execute in that context.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 1
    Yes, but you still need a launch image or launch screen before the video plays and that will need to be created in Interface Builder or be a simple image – Paulw11 Sep 30 '15 at 08:39
  • 1
    Of course - I suppose it depends on whether transitioning from a static image into a dynamic image would work for the OP... – Grimxn Sep 30 '15 at 08:58
  • The OP's question was avoiding the use of IB to create the launch screen – Paulw11 Sep 30 '15 at 09:15
6

This doesn't really count as programmatically since it's just editing the info.plist file but, based on my research I found that there is a new way being introduced especially now with SwiftUI and how it will be replacing StoryBoards that I found here

Picture from the source

Thought I share it here for people searching this topic and come across this post for the first time and who just want a different way than using StoryBoards to create their Launch Screens (like me).

shadow of arman
  • 395
  • 1
  • 7
  • 16
-7

It works successfully

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
}