我无法在 StackView 中添加 UIProgressView
I can't add UIProgressView in StackView
根据这段代码,我需要在StackView
中添加4个UIProgressViews
,但在StackView
中只添加了1个UIProgressView
。我正在使用 Horizontal
StackView
.
代码:
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var stackView: UIStackView!
var progressView: UIProgressView = {
var view = UIProgressView()
view.tintColor = .white
view.progress = 0.5
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
for _ in 1...4 {
stackView.addArrangedSubview(progressView)
}
}
}
您要添加相同的 UIProgressView
四次。但是当你向视图添加子视图时,子视图首先从其现有的父视图中删除,因为子视图只能有一个父视图并且只能占用其父视图的subviews
数组中的一个插槽。
您需要创建四个 UIProgressView
。
let progressViews: [UIProgressView] = (0 ..< 4).map { _ in
let view = UIProgressView()
view.tintColor = .white
view.progress = 0.5
return view
}
override func viewDidLoad() {
super.viewDidLoad()
for progressView in progressViews {
stackView.addArrangedSubview(progressView)
}
}
根据这段代码,我需要在StackView
中添加4个UIProgressViews
,但在StackView
中只添加了1个UIProgressView
。我正在使用 Horizontal
StackView
.
代码:
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var stackView: UIStackView!
var progressView: UIProgressView = {
var view = UIProgressView()
view.tintColor = .white
view.progress = 0.5
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
for _ in 1...4 {
stackView.addArrangedSubview(progressView)
}
}
}
您要添加相同的 UIProgressView
四次。但是当你向视图添加子视图时,子视图首先从其现有的父视图中删除,因为子视图只能有一个父视图并且只能占用其父视图的subviews
数组中的一个插槽。
您需要创建四个 UIProgressView
。
let progressViews: [UIProgressView] = (0 ..< 4).map { _ in
let view = UIProgressView()
view.tintColor = .white
view.progress = 0.5
return view
}
override func viewDidLoad() {
super.viewDidLoad()
for progressView in progressViews {
stackView.addArrangedSubview(progressView)
}
}