所以我的班级看起来像这样:
import UIKit
class GradientBarView: UIView {
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
let gradient = CAGradientLayer()
gradient.frame = bounds
gradient.startPoint = CGPoint(x: 0, y: 0.5)
gradient.endPoint = CGPointMake(1, 0.5)
gradient.colors = [AppColor.SecondaryGreen.CGColor, AppColor.HeritageGreen.CGColor, AppColor.HeritageGreen.CGColor]
layer.insertSublayer(gradient, atIndex: 0)
translatesAutoresizingMaskIntoConstraints = false
}
}
这就是我使用它的方式(在我的viewDidLoad中)
let gradientBar = GradientBarView(frame: CGRectZero)
self.view.addSubview(gradientBar)
然后添加必要的约束(这很好)...所以问题是视图只是黑色但是如果我通过Storyboard添加它就可以正常工作
请帮助,我缺少什么?
由于
答案 0 :(得分:2)
当你在commonInit中调用gradient.frame = bounds时,你的框架是CGRectZero。因此你的界限也将是CGRectZero。因此,您插入的框架没有高度或宽度。
您可以在自定义视图的draw方法中添加渐变。 override func draw(_ rect: CGRect)
只需确保不要多次添加。
或者将其添加到layoutSubviews,具体取决于您的用途。类似的东西:
class GradientBarView: UIView {
var gradientLayer: CAGradientLayer?
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
translatesAutoresizingMaskIntoConstraints = false
}
override func layoutSubviews() {
super.layoutSubviews()
gradientLayer?.removeFromSuperlayer()
gradientLayer = self.addHorizontalGradient(color: UIColor.blue)
}
}
extension UIView {
func addHorizontalGradient(color: UIColor) -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.startPoint = CGPoint(x:0,y:0.5)
gradient.endPoint = CGPoint(x:1,y:0.5)
gradient.colors = [color.cgColor, UIColor.white.withAlphaComponent(0).cgColor]
self.layer.insertSublayer(gradient, at: 0)
return gradient
}
}
答案 1 :(得分:0)
我有同样的问题。如果我在界面生成器上向viewController添加视图,则会显示渐变。但是,如果我在这种情况下以编程方式进行操作,则不会出现渐变。 这是无法正常运行的代码。
<div class="container">
<div class="images">
<img src=https://picsum.photos/id/1010/400/300 />
<img src=https://picsum.photos/id/1011/400/300 />
<img src=https://picsum.photos/id/1012/400/300 />
<img src=https://picsum.photos/id/1013/400/300 />
</div>
</div>
并将 setGradientBackground()函数调用从 setupView()移到hext方法中,即可解决此问题。
import UIKit
class HeaderLandscape: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupFromNib()
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupFromNib()
setupView()
}
private func setupView() {
setGradientBackground()
}
private func setGradientBackground() {
let gradient = CAGradientLayer()
gradient.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: self.bounds.height)
gradient.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.32).cgColor,
UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor]
gradient.startPoint = CGPoint(x: 0.5, y: 0)
gradient.endPoint = CGPoint(x: 0.5, y: 1)
self.layer.insertSublayer(gradient, at: 0)
}
}