Creating LaunchScreen without Storyboard.

Create a LaunchScreenView class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import UIKit

class LaunchScreenView: UIView {

lazy var nameLabel : UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 28, weight: .semibold)
label.text = "My Test APP"
label.textColor = .black
label.textAlignment = NSTextAlignment.center
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.sizeToFit()

self.addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: self.centerXAnchor),
label.topAnchor.constraint(equalTo: self.logoImageView.bottomAnchor, constant: 25)
])

return label
}()

lazy var logoImageView : UIImageView = {
let imgView = UIImageView()
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.image = UIImage(named: "launch_icon")
self.addSubview(imgView)

NSLayoutConstraint.activate([
imgView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -25),
imgView.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0)
])
return imgView
}()

override init(frame: CGRect) {
super.init(frame: frame)

let _ = self.nameLabel
self.backgroundColor = .orange
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

Add the LauchScreenView in SceneDelegate

1
2
3
4
5
6
7
8
9
10
11
12
13
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
...

let launchView = LaunchScreenView(frame: UIScreen.main.bounds)
window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
window?.rootViewController?.view.addSubview(launchView)
window?.rootViewController?.view.bringSubviewToFront(launchView)
window?.makeKeyAndVisible()

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
self.window?.rootViewController = ViewController()
}
}