简体   繁体   English

在所有子视图具有自动布局之前调用viewWillTransitionToSize

[英]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.

相关问题 子视图何时完全正确地布置? - When are subviews completely, correctly laid out? 在什么时候UIViewController的子视图最初确定了? - At what point are a UIViewController's subviews definitely laid out initially? 修改已经使用自动布局布局的情节提要 - Modifying a storyboard that is already laid out with auto layout 在iPad Simulator中未调用viewWillTransitionToSize - viewWillTransitionToSize not called in iPad Simulator iOS:用于实现指令叠加:需要先布局视图层次结构的一个分支,然后再布局另一分支 - iOS: For implementing instruction overlay: need one branch of view hierarchy to be laid out before another branch is laid out 自动布局超级视图中的手动布局视图 - Manually laid out view in auto layout super view viewWillTransitionToSize:withTransitionCoordinator: 不会被自动调用 - viewWillTransitionToSize:withTransitionCoordinator: Not being called automatically UIView使用自动布局拥抱所有子视图 - UIView hug all subviews using Auto Layout 在所有视图控制器上调用viewWillTransitionToSize - viewWillTransitionToSize is invoked on all the view controllers 如何在iOS的uiviewcontroller中列出所有子视图? - How to list out all the subviews in a uiviewcontroller in iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM