gpt4 book ai didi

ios - UIView 框架和边界错误

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:31 24 4
gpt4 key购买 nike

我有一个 UIView 子类,我正在向其添加一些 CALayers。我已经通过 Storyboard 将此 UIView 添加到我的 View 中。出于某种原因,在 init(和 awakeFromNib)中访问框架和边界总是 (0, 0, 1000, 1000)。这是为什么?

class SliderView: UIView {
let trackLayer = CAShapeLayer()

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// The bounds are wrong
trackLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: 15).cgPath

}
}

最佳答案

我在 UITableViewCell 中遇到了同样的问题。此解决方法应该有效

override func drawRect(rect: CGRect) {
super.drawRect(rect)
customInit()
}

var initialized = false
func customInit(){
if !initialized{
initialized = true

// Bounds will be good. Do your stuff here
}
}

在你的情况下它应该是这样的:

class SliderView: UIView {
let trackLayer = CAShapeLayer()

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// The bounds are wrong
}

override func drawRect(rect: CGRect) {
super.drawRect(rect)
customInit()
}

var initialized = false
func customInit() {
if !initialized {
initialized = true

// Bounds will be good.
trackLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: 15).CGPath
}
}
}

关于ios - UIView 框架和边界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39473791/

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