我想问一下为什么UIView(背景颜色为橙色)不会出现的原因。
https://imgur.com/kAtelaE此链接包含一张图片,其中显示UIView预览。 https://imgur.com/E2xOWb7此链接的图片显示UIView不会出现在模拟器中。
下面是ViewController代码。我已经知道gradientLayer的Z轴为负数。
extension UIColor {
convenience init(hexString: String) {
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (255, 0, 0, 0)
}
self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}
class ViewController: UIViewController {
@IBOutlet weak var playerView: UIView!
@IBOutlet weak var controlView: UIView!
@IBOutlet weak var controlPanel: UIView!
@IBOutlet weak var progressView: UIView!
// playerView에서 gradient background color
var gradientLayer: CAGradientLayer!
func createGradientLayer() {
gradientLayer = CAGradientLayer()
gradientLayer.frame = playerView.bounds
gradientLayer.colors = [UIColor(hexString: "#385175").cgColor, UIColor(hexString: "#36a9ba").cgColor]
gradientLayer.zPosition = -2
playerView.layer.addSublayer(gradientLayer)
}
override func viewDidLoad() {
super.viewDidLoad()
controlPanel.layer.cornerRadius = 7.0
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
createGradientLayer()
}
}
如何使UIView出现? 感谢您的阅读。