How to Code the Launchscreen Programmatically

Is there any way to code the LaunchScreen programmatically

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.

Programmatically accessing the launch screen XIB or storyboard

As Xib is not in the main bundle getting path returns nil, But you can get the XIB of the launch screen without the help of path using method

  let launchScreenNib = UINib(nibName: "LaunchScreen", bundle: nil)

or

You can load get views from XIB as

// Swift
let objects = NSBundle.mainBundle().loadNibNamed("LaunchScreen", owner: self, options: nil)
let view = objects[0] as UIView

// Obj C
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"LaunchScreen" owner:self options:nil];
UIView *view = [objects objectAtIndex:0];

Execute Code in Launch Screen UPDATED

It is not possible to write any custom class/code for the launchscreen xib/storyboard files. We can only design using resource files.

Execute code in Launch Screen

No, it's not possible.

When launch screen is being displayed your app will be in loading state.

Even the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will not be completely executed while the launch screen is displayed.

So it's clear that, you don't have any access to your app and so at this point you can't execute any code.

How can I delay splash launch screen programmatically in Swift Xcode iOS

As of today there is no predefine method from Apple to hold launch screen. Here are some Approaches which are not optimum but works

Approach #1
Create a Separate ViewController which has Launch logo & create a timer or perform some operation (Like Database/Loads some essential network call) depends on your app type this way you can ready with data before hand & hold the launch screen as well :)

Approach #2 Not Optimum

Use Sleep code which holds up the app for a while.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}

Add a Launch Screen to the iOS App in Code in Swift (without Storyboard or Launchboard)

This is not possible as such as the Launch Screen is not a regular View Controller and it cannot hold any logic. Any animations which are there in the App during startup are handled after the launch Screen has appeared and the animations are in the root view controller as in Twitter app. If you are completely against using launch Screens then an alternative would be make a view in IB and use its screenShot as a splashImage. There is no way as of now to make a launchScreen programatically or to add any logic to it.

Add Launch Screen Image instead of using LaunchScreens.storyboard for iOS

Since the question included "use a View or and UIImage instead?"

From Apple's docs...

Static Launch Screen Images

It’s best to use an Xcode storyboard for your launch screen, but you can provide a set of static images if necessary.

Reference page, including a list of required image sizes: https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/

Note: To use images instead of a storyboard...

In Xcode open your Assets catalog (or create a new one), right-click in the assets section and select App Icons & Launch Images -> New iOS Launch Image and it will create the proper template. Also go to project General settings and select Launch Images Source - Use Asset Catalog...



Related Topics



Leave a reply



Submit