iOS custom gradient background UIView won't display in simulator [duplicate]

家住魔仙堡 提交于 2019-12-25 18:11:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!