获取 UIStackView 子视图的框架
Get the frame of a UIStackView subViews
我在 IB 中创建了一个 UIStackView,它的分布设置为 Fill Equally。我正在寻找每个子视图的框架,但下面的代码总是 returns (0, 0, 0, 0).
class ViewController: UIViewController {
@IBOutlet weak var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
let pView = UIView()
let sView = UIView()
pView.backgroundColor = UIColor.red
sView.backgroundColor = UIColor.orange
stackView.addArrangedSubview(pView)
stackView.addArrangedSubview(sView)
}
override func viewDidLayoutSubviews() {
print(stackView.arrangedSubviews[0].frame)
print(stackView.arrangedSubviews[1].frame)
}
}
我认为设置为平均填充的堆栈视图会自动设置计算它。
如有任何帮助,我们将不胜感激。
看完你的代码我认为这只是对viewDidLayoutSubviews()的误解。基本上当主视图的所有后代视图都已布局时调用它,但这不包括这些视图的子视图(后代)。请参阅 Apple 的讨论 notes。
"When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout."
话虽这么说,现在有很多方法可以获取子视图的框架。
首先,您可以在 viewdidload 中添加一行代码并获取它。
override func viewDidLoad() {
super.viewDidLoad()
let pView = UIView()
let sView = UIView()
pView.backgroundColor = UIColor.red
sView.backgroundColor = UIColor.orange
stackView.addArrangedSubview(pView)
stackView.addArrangedSubview(sView)
stackView.layoutIfNeeded()
print(stackView.arrangedSubviews[0].frame)
print(stackView.arrangedSubviews[1].frame)
}
或者您可以等到 viewDidAppear 并在那里查看。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(stackView.arrangedSubviews[0].frame)
print(stackView.arrangedSubviews[1].frame)
}
我在 IB 中创建了一个 UIStackView,它的分布设置为 Fill Equally。我正在寻找每个子视图的框架,但下面的代码总是 returns (0, 0, 0, 0).
class ViewController: UIViewController {
@IBOutlet weak var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
let pView = UIView()
let sView = UIView()
pView.backgroundColor = UIColor.red
sView.backgroundColor = UIColor.orange
stackView.addArrangedSubview(pView)
stackView.addArrangedSubview(sView)
}
override func viewDidLayoutSubviews() {
print(stackView.arrangedSubviews[0].frame)
print(stackView.arrangedSubviews[1].frame)
}
}
我认为设置为平均填充的堆栈视图会自动设置计算它。
如有任何帮助,我们将不胜感激。
看完你的代码我认为这只是对viewDidLayoutSubviews()的误解。基本上当主视图的所有后代视图都已布局时调用它,但这不包括这些视图的子视图(后代)。请参阅 Apple 的讨论 notes。
"When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout."
话虽这么说,现在有很多方法可以获取子视图的框架。
首先,您可以在 viewdidload 中添加一行代码并获取它。
override func viewDidLoad() {
super.viewDidLoad()
let pView = UIView()
let sView = UIView()
pView.backgroundColor = UIColor.red
sView.backgroundColor = UIColor.orange
stackView.addArrangedSubview(pView)
stackView.addArrangedSubview(sView)
stackView.layoutIfNeeded()
print(stackView.arrangedSubviews[0].frame)
print(stackView.arrangedSubviews[1].frame)
}
或者您可以等到 viewDidAppear 并在那里查看。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(stackView.arrangedSubviews[0].frame)
print(stackView.arrangedSubviews[1].frame)
}