我有两个视图控制器A和B。我可以通过Segue从A转到B,并且通过实现UISegue类在Segue上实现了淡入淡出的动画效果。但是,在设备旋转和展开时会发生问题(从B到A,而在B时发生旋转)。
出现的问题是要显示的视图(视图A)的布局错误。在转换无效之前,请调用诸如setNeedsUpdateConstraints()
或layoutSubviews()
之类的函数或类似函数。屏幕截图描述了A在风景中打开然后对B进行定位的情况。在B中时,设备会旋转回纵向,然后退绕到A。
segues的Swift代码:
class SegueFadeOut: UIStoryboardSegue {
override func perform() {
UIView.transition(from: source.view, to: destination.view, duration: 5.5, options: [.transitionCrossDissolve]) { (success) in
self.destination.dismiss(animated: false, completion: nil)
}
}
}
class SegueFadeIn: UIStoryboardSegue {
override func perform() {
let toViewController = self.destination
let fromViewController = self.source
let containerView = fromViewController.view.superview
toViewController.view.alpha = 0.0
containerView?.addSubview(toViewController.view)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
toViewController.view.alpha = 1.0
}, completion: { success in
fromViewController.present(toViewController, animated: false, completion: nil)
})
}
}
关于如何或在何处实现可解决此布局问题的功能的任何线索?谢谢!
答案 0 :(得分:0)
我设法修复了它。我可以在执行动画之前设置帧。我还停止使用UIView.transition(...)
函数,因为它在关闭fromViewController时出现了一些错误。
class SegueFadeOut: UIStoryboardSegue {
override func perform() {
let toViewController = self.destination
let fromViewController = self.source
let containerView = fromViewController.view.superview
toViewController.view.alpha = 0.0
// ADDED LINE BELOW
toViewController.view.frame = fromViewController.view.frame
containerView?.addSubview(toViewController.view)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
toViewController.view.alpha = 1.0
}, completion: { success in
fromViewController.dismiss(animated: false, completion: nil)
})
}
}