这让我很疯狂。只有在第一次运行时才会调用viewDidLayoutSubviews两次。
以下是我正在使用的代码:
class CICViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func addQLabel(qLabel: UILabel, questionString: String, bgrLabel: UILabel) {// some code goes here
}
func makeRoundQButtons(qButtons:[UIButton]) {
// some code goes here
}
func addArrows(numberOfArrows:Int, buttonCurAngle:Double) {//some code goes here
}
func animateButtons(qButtons:[UIButton], buttonCurAngle:Double) {
// some code goes here
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
func backTapped(sender: UIBarButtonItem) {
navigationController?.popViewControllerAnimated(false)
//some code goes here
}
func restartTapped(sender: UIBarButtonItem) {
navigationController?.popToRootViewControllerAnimated(false)
//some code goes here
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
在我的ViewController中,我称之为:
class OneViewController: CICViewController {
override func viewDidLoad() {
super.viewDidLoad()
//some code goes here
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("viewDidLayoutSubviews")
self.makeRoundQButtons(qButtons)
self.animateButtons(qButtons, buttonCurAngle: 2.0)
}
override func viewDidAppear(animated: Bool) {
//nothing goes here
}
}
答案 0 :(得分:4)
无法保证调用viewDidLayoutSubviews
的次数。
您可以在此Stack Overflow帖子中找到一个很好的讨论:
When is layoutSubviews called?
答案 1 :(得分:1)
我发现this article很有用。它的内容摘要如下:
init
不会导致layoutSubviews
被调用(duh)- 只有当框架的尺寸参数不同时,
addSubview
导致在添加的视图,要添加到的视图(目标视图)以及目标视图的所有子视图中调用layoutSubviews
setFrame
智能地调用layoutSubviews
视图上的UIScrollView
- 滚动
layoutSubviews
会导致在scrollView
上调用layoutSubview
,这是超级视图- 旋转设备仅在父视图(响应
viewControllers
主视图)上调用removeFromSuperview
layoutSubviews
- 仅在超级视图中调用{{1}}(未在表格中显示)