[英]Gradient not filling IBDesignable UIView
我試圖在@IBDesignable UIView上添加漸變。 在Simulator中一切都很好,但是當我在物理設備上運行它時,漸變不會充滿屏幕。 我在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中的漸變不會填充整個屏幕。
我嘗試從MyViewController
的viewDidLoad
, viewDidLayoutSubviews
, viewDidAppear
運行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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.