gpt4 book ai didi

Swift:将 subview 添加到另一个 subview

转载 作者:行者123 更新时间:2023-11-28 12:16:20 29 4
gpt4 key购买 nike

我正在努力向 View 添加 subview 。进度变量在其 View Controller 的 viewDidLoad() 中设置。 progressView frame size 设置正确,只是添加了好像出错的subview。知道缺少什么吗?

class ProgressBar: UIView {
var progressView = UIView()
var progress: CGFloat? {
didSet {
setProgressBar(progress: progress!)
}
}

func setProgressBar(progress: CGFloat) {
self.layer.masksToBounds = true
let progressViewWidth: CGFloat = self.frame.width * progress
progressView.backgroundColor = .red
progressView.frame = CGRect(origin: frame.origin,
size: CGSize(width: progressViewWidth, height: frame.height))
self.addSubview(progressView)
}
}

最佳答案

progressView.frame = CGRect(origin: frame.origin, size: CGSize(width: progressViewWidth, height: frame.height))

应该这样替换

progressView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: progressViewWidth, height: frame.height))

progressView的位置与progressBar的有关

关于Swift:将 subview 添加到另一个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433029/

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