渐变不填充IBDesignable UIView

时间:2017-06-03 12:45:48

标签: ios swift uiview cagradientlayer ibdesignable

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

extension UIView {
    func gradientFrom(_ colors: [UIColor]) {
        let cgColors = colors.map({return $0.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($0)})
        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中的渐变不会填满整个屏幕。

我尝试从applyBackgroundGradient MyViewController viewDidLoadviewDidLayoutSubviewsviewDidAppear运行{{1}},但这些似乎都没有完成任务。

任何建议:如何解决这个问题非常感谢。谢谢你的阅读。

1 个答案:

答案 0 :(得分:1)

尝试替换

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)
}