gpt4 book ai didi

ios - 渐变不填充 IBDesignable UIView

转载 作者:行者123 更新时间:2023-11-28 15:35:30 25 4
gpt4 key购买 nike

我正在尝试在@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com