[英]viewWillTransitionToSize called before all subviews have auto-laid-out
I have a View Controller with a view hierarchy within two nested views that are placed with autolayout constraints. 我有一个视图控制器,其中的视图层次结构位于两个嵌套视图中,这些嵌套视图具有自动布局约束。 I then place in code some UIViews into one of those views but I don't want to use constraints for these as the user can drag them around.
然后,我将一些UIViews放入代码中的其中一个视图中,但是我不想为它们使用约束,因为用户可以拖动它们。
If the device gets rotated, or the user splits the view using the slide over functionality of the iPad I want to reset the locations of these UIViews into their new place based on the automatic layout of the parent views: 如果设备旋转,或者用户使用iPad的滑盖功能拆分视图,我想根据父视图的自动布局将这些UIView的位置重置到新位置:
I have a function in the questionView called resetAnswerPosition() that correctly positions all of the Views based on the position of the reference View after the auto-layout positioning has kicked in. QuestionView has stores a reference to the ReferenceView, to make this easier. 我在questionView中有一个函数称为resetAnswerPosition(),该函数可以在自动布局定位开始后根据参考View的位置正确定位所有View。QuestionView存储了对ReferenceView的引用,以简化此操作。
The documentation suggests using viewWillTransitionToSize & willTransitionToTraitCollection to run these changes which works for rotation, or for when the slide over changes the aspect ratio from landscape to portrait or back again. 该文档建议使用viewWillTransitionToSize和willTransitionToTraitCollection来运行这些更改,这些更改适用于旋转,或者当滑行将纵横比从横向更改为纵向或再次返回时适用。 However on an iPad Pro when change the split from 2/3 to 1/2 the aspect ratio isn't changing that the resetAnswerTilesPositions is being called before the Reference View has been resized.
但是,在iPad Pro上,将分割比例从2/3更改为1/2时,宽高比不会改变,即在调整参考视图的大小之前会调用resetAnswerTilesPositions。
I have also tried to use the viewDidLayoutSubViews but again have the issue that this is run before the Reference view has been resized. 我也尝试过使用viewDidLayoutSubViews,但又遇到了在调整参考视图大小之前运行此问题。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (context) -> Void in
self.questionView.resetAnswerTilePositions()
},
completion: { (context) -> Void in
})
}
override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.questionView.resetAnswerTilePositions()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.questionAnswerView.resetAnswerTilePositions()
}
What function can I register either with the View Controller or the QuestionView which will only be called once all of the views have been laid out according to the updated constraints? 我可以在视图控制器或QuestionView中注册什么功能,只有在根据更新后的约束对所有视图进行布局后才可以调用QuestionView?
As always posting a question led me to the right answer. 与往常一样,发布问题使我找到了正确的答案。
In the Question View you can override the layoutSubviews routine which is really nice from a separation of concerns of point of view. 在问题视图中,您可以重写layoutSubviews例程,从关注点分离的角度来看,这确实很棒。 Only the Question View then needs to know how to layout its own subviews.
然后,只有问题视图才需要知道如何布局其自己的子视图。
override func layoutSubviews() {
super.layoutSubviews()
self.resetAnswerTilePositions()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.