渐变不填充 IBDesignable UIView

Gradient not filling IBDesignable UIView

我正在尝试在@IBDesignable UIView 上添加渐变。在模拟器中一切都很好,但是当我 运行 它在物理设备上时,渐变不会填满屏幕。我在 UIView 扩展中有我的渐变代码:

extension UIView {
    func gradientFrom(_ colors: [UIColor]) {
        let cgColors = colors.map({return [=10=].cgColor})
        let gradient = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.locations =  [0.0, 1.0]
        gradient.colors = cgColors
        self.layer.insertSublayer(gradient, at: 0)
    }
}

接下来,我的 UIView 中以渐变为中心的代码:

@IBDesignable class MyCustomView: UIView {

    var view = UIView()

    // MARK: - IBOutlets
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var textView: UITextView!

    fileprivate var gradientColors: [UIColor] = []

    @IBInspectable var gradientColor0: UIColor = UIColor.flatWhite {
        didSet {
            gradientColors.remove(at: 0)
            gradientColors.insert(gradientColor0, at: 0)
        }
    }

    @IBInspectable var gradientColor1: UIColor = UIColor.flatWhiteDark {
        didSet {
            gradientColors.remove(at: 1)
            gradientColors.insert(gradientColor1, at: 1)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
        setup()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }

    func setup() {
        textView.scrollRangeToVisible(NSMakeRange(0, 0))
        [gradientColor0, gradientColor1].forEach({gradientColors.append([=11=])})
        view.gradientFrom(gradientColors)
        label.textColor = labelFontColor
        label.backgroundColor = UIColor.clear
        textView.backgroundColor = UIColor.clear
        textView.textColor = textViewFontColor
    }

    // MARK: - Nib handlers

    fileprivate func xibSetup() {
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)
    }

    fileprivate func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "AttributionView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }

    public func applyBackgroundGradient() {        
        view.gradientFrom(gradientColors)
    }

    func resizeView() {
        textView.sizeToFit()
        textView.scrollRangeToVisible(NSMakeRange(0, 0))
    }
}

在 MyViewController 上,我添加了一个 UIView,将其子类设置为 MyCustomView。界面生成器会填充,但是当我在物理设备上转到 运行 时,MyCustomView 中的渐变不会填满整个屏幕。

我尝试了 运行宁 applyBackgroundGradientMyViewControllerviewDidLoad, viewDidLayoutSubviews, viewDidAppear, none这些似乎完成了工作。

非常感谢有关如何解决此问题的任何建议。感谢阅读。

尝试替换

gradient.frame = self.bounds

gradient.frame.size = self.frame.size
gradient.frame.origin = CGPoint(x: 0.0, y: 0.0)

你绝对应该这样称呼它

var didLayoutSubviews = false

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    if !self.didLayoutSubviews {
         self.didLayoutSubviews = true
         //apply gradient
         //you can also try to execute drawing inside `DispatchQueue.main.async {}`
    }
}

OP 的附录:

我的部分问题源于我对渐变颜色的 didSet 调用。我没有在 didSet 中设置数组,而是将填充 gradientColors 数组的代码移到了 applyBackgroundGradient 方法中。

@IBInspectable var gradientColor0: UIColor = UIColor.flatWhite {
    didSet {
        applyBackgroundGradient()
    }
}

@IBInspectable var gradientColor1: UIColor = UIColor.flatWhiteDark {
    didSet {
        applyBackgroundGradient()
    }
}

...MyCustomView 上的渐变代码如下所示:

public func applyBackgroundGradient() {
    gradientColors = [gradientColor0, gradientColor1] 
    self.view.gradientFrom(gradientColors)
}