ios - 渐变不填充 IBDesignable UIView

标签 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 中的渐变不会填满整个屏幕。

我尝试从 MyViewControllerviewDidLoadviewDidLayoutSubviewsviewDidAppear 运行 applyBackgroundGradient >,这些似乎都无法完成工作。

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

最佳答案

尝试替换

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

关于ios - 渐变不填充 IBDesignable UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44344090/

相关文章:

ios - 如何摆脱内部使用异步DispatchQueue的循环

ios - 如何向 UIView 添加大小调整 handle ?

iphone - TabBarController的框架受模态弹出的影响

ios - Swift 中的 UIPageViewController

swift - 按钮在程序化动态 stackView 中变为非事件状态 (Swift)

swift - 检查字典值的更好方法

ios - 在哪里可以获得 SRVResolver 示例代码?

ios - 延迟显示一个字母

ios - 如何使 UILabel 出现和消失 5 次?

ios - UISegmentedControl 以编程方式