Xcode allows to create launch screen in .xib
files via Interface Builder. Is it possible to execute some code with the xib, just like in usual view controllers? It would be great if we can set different text/images/etc while app launching.
-
4Think about **why** the launch screen is there. – zaph Dec 24 '14 at 20:53
-
@Zaph I understand WHY. But in other side, if i want to show just random greeting from defined list it will take nothing to iOS. – Dmitriy Dec 24 '14 at 21:02
-
2You could always add an initial view controller that displays the same image as the launch screen, and add your greetings to that. – rdelmar Dec 24 '14 at 23:39
-
@Dmitriy, I've seen lots of apps do this, their launching screen changes images and text based on the day, the greeting messages changes too, I just don't know how they did it. – RainCast Sep 13 '16 at 04:23
-
Check my answer bellow. :) – RainCast Sep 13 '16 at 05:52
-
RainCast: The code you have posted along with your answer is unnecessary. Check @rdelmar s comment, what he is suggesting is the easiest way to achieve the mentioned behaviour. – Midhun MP Sep 13 '16 at 13:57
4 Answers
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.

- 103,496
- 31
- 153
- 200
-
Thank you for the response. So the launch xib loaded by iOS, not by the app. Sadly, because I thought about showing different greetings with different launches. – Dmitriy Dec 24 '14 at 21:18
-
@Dmitriy: Sadly it is not possible, Just launch image is replaced by a launch xib. Only advantage is, you can design on it. But can't execute any code. – Midhun MP Dec 24 '14 at 21:20
-
-
1@RainCast: No, it's not possible. You can't execute any code in launch screen xib. The answer you are posting doesn't answer the question (OP asks how to execute code in launch screen xib). Also the code you have written is unnecessary. The effect you are mentioning can be achieved by simply designing your initial view controller (in the storyboard) similar to launch screen xib and write the animation logic in that view controller class. When you finish doing animation just segue to next page programmatically. – Midhun MP Sep 13 '16 at 13:53
-
@MidhunMP, I see your point now. In this case my answer addressed this point - "It would be great if we can set different text/images/etc while app launching", this is what the OP wanted to achieve. Your answer is great too, thanks for sharing. – RainCast Sep 13 '16 at 18:00
I was trying to do the same thing here. :)
I really liked some of the apps, in which they do a little dynamic greeting text and image each time the app is launched, such as "You look good today!", "Today is Friday, a wonderful day", etc, which is very cute.
I did some search, below is how to do it: (My code is XCode 7, with a launchscreen.xib file)
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var customizedLaunchScreenView: UIView?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
application.statusBarHidden = true
// customized launch screen
if let window = self.window {
self.customizedLaunchScreenView = UIView(frame: window.bounds)
self.customizedLaunchScreenView?.backgroundColor = UIColor.greenColor()
self.window?.makeKeyAndVisible()
self.window?.addSubview(self.customizedLaunchScreenView!)
self.window?.bringSubviewToFront(self.customizedLaunchScreenView!)
UIView.animateWithDuration(1, delay: 2, options: .CurveEaseOut,
animations: { () -> Void in
self.customizedLaunchScreenView?.alpha = 0 },
completion: { _ in
self.customizedLaunchScreenView?.removeFromSuperview() })
}
return true
}
// other stuff ...
}
Just do what ever you wanted to show, text, images, animations, etc. inside the customizedLaunchScreenView here.
At the end of the launching, just fade out this customized UIView using alpha value change, then remove it completely.
How cool is that? I absolutely love it!
Hope it helps.

- 4,134
- 7
- 33
- 47
-
5didFinishLaunchingWithOptions is called when the app has finish to load, so after the launchscreen is displayed. I understand why ones would want a custom transition before going into the app but this isn't a custom launchscreen in any way. – thibaut noah Mar 30 '17 at 08:58
-
So it's not the exact answer to the question.. but it's very helpful.. a nice workaround – Yitzchak May 09 '17 at 13:58
I was also trying to achieve this. I tried the following, it gives a delay of couple of seconds but works for me.
- Create and set a launch screen in project settings.
- Create a view controller with custom class (SplashViewController) and set it your starting view controller in storyboard.
- Add a container view in it and set it to full screen.
- Set embedded segue to a Storyboard Reference.
- Select Storyboard Reference and set launch screen in StoryBoard property from Attribute inspector.
- Do whatever you want in SplashViewController (play animation or session check etc) and perform segue when done.
Hope it helps!

- 187
- 1
- 12
Language: Swift 4
Hello, here's a solution for using xib for launch screen.
Adding new class named LaunchView to your project, then u'll have a new file LaunchView.swift in project.
Go to LauchImage.xib to set class to LaunchView (which it's your new class).
Adding some code, & reading some information show at the LaunchView.swift. Here's is my code for displaying version of app at launch screen.
class LaunchView: UIView {
lazy var versionLabel: UILabel = {
let label = Factory.label(style: LabelStyle.custom(font: .regular, size: 10, color: .other(ColorUtility.versionGray)), mutiLine: false, textAlignment: .center)
label.text = "ver \(Constants.appVersion)"
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
nibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
nibSetup()
}
private func nibSetup() {
addSubview(versionLabel)
versionLabel.snp.makeConstraints { (make) in
make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom).offset(-6)
make.centerX.equalToSuperview()
}
}
}

- 81
- 4