Ios 渐变不填充可设计UIView
我试图在@IBDesignable UIView上添加渐变。在模拟器中一切都很好,但当我在物理设备上运行它时,渐变并没有填满屏幕。我的渐变代码在UIView扩展中:Ios 渐变不填充可设计UIView,ios,swift,uiview,cagradientlayer,ibdesignable,Ios,Swift,Uiview,Cagradientlayer,Ibdesignable,我试图在@IBDesignable UIView上添加渐变。在模拟器中一切都很好,但当我在物理设备上运行它时,渐变并没有填满屏幕。我的渐变代码在UIView扩展中: extension UIView { func gradientFrom(_ colors: [UIColor]) { let cgColors = colors.map({return $0.cgColor}) let gradient = CAGradientLayer()
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。Interface builder会填充,但当我在物理设备上运行它时,MyCustomView中的渐变不会填充整个屏幕
我尝试从MyViewController
的viewdiload
,viewdilayoutsubviews
,viewdileasen
运行applyBackgroundGradient
,但这些似乎都没有完成任务
任何关于:如何解决这一问题的建议都将不胜感激。感谢阅读。尝试替换
gradient.frame = self.bounds
与
你绝对应该这样称呼它
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)
}
您是否在MyCustomView上设置了限制,以便它填充屏幕?是的。我在模拟器中得到了预期的结果。我在一个物理设备上遇到了这个问题。你在模拟器上得到了所有设备大小的理想结果?你什么时候调用
applyBackgroundGradient
?它应该在viewDidLayoutSubviews
中工作,您是否调用过它?谢谢!我的部分问题是框架的问题。另一部分是我应用梯度的方式。我在渐变色系列中做了太多的工作。相反,我将数组填充代码下移到了applyBackgroundGradient()
。我将添加到您答案的底部。@Adrian,gradientColors=[gradientColor0,gradientColor1]
应该足够了。在viewDidLayoutSubviews中添加渐变对我来说是个解决办法!谢谢
public func applyBackgroundGradient() {
gradientColors = [gradientColor0, gradientColor1]
self.view.gradientFrom(gradientColors)
}