问题
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
}
}
回答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.
来源:https://stackoverflow.com/questions/49980213/ios-custom-gradient-background-uiview-wont-display-in-simulator