0

I have a custom gradient background on my launch screen which looks and works fine in Interface Builder in XCode but won't display in simulator. What's happening?

import UIKit

@IBDesignable
open class LaunchBkg: UIView {


private lazy var gradientLayer: CAGradientLayer = {
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = self.bounds
    gradientLayer.colors = [#colorLiteral(red: 0.2588235294, green: 0.8, blue: 0.8352941176, alpha: 1).cgColor, #colorLiteral(red: 0, green: 0.5764705882, blue: 0.9098039216, alpha: 1).cgColor]
    return gradientLayer
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    layer.insertSublayer(gradientLayer, at: 0)
}

public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    layer.insertSublayer(gradientLayer, at: 0)
}

open override func layoutSubviews() {
    super.layoutSubviews()
    gradientLayer.frame = bounds
}
}

Display in Interface Builder

Simulator

Sean
  • 629
  • 8
  • 24
  • Did you check bounds at the time of adding for both, I have a feeling that in once case bounds are 0,0,0,0 or something like that... – Ladislav Apr 23 '18 at 11:52
  • Your code is working well in my case. Remove and add again UIView. – Jay Patel Apr 23 '18 at 11:53
  • Your code is working , Please check call this in parent view. – a.masri Apr 23 '18 at 11:58
  • I've just created a test project and seen the gradient works fine except on launch screen where I'm trying to use it. Do I need to use an image for the launch screen? – Sean Apr 23 '18 at 11:59
  • @Sean LaunchScreen won't run any code. You have to use an image in storyboard for that. – Kowboj Apr 23 '18 at 12:02

1 Answers1

1

Do I need to use an image for the launch screen

Yes.

The launch screen is just an image that appears while your app is launching. By definition, your app has not launched yet; that is why we need a launch screen, to cover the gap of time before your code runs. Thus, also by definition, your code has not yet begun to run. You cannot affect the launch screen by means of code.

matt
  • 515,959
  • 87
  • 875
  • 1,141