不要害怕随之而来的巨大代码。您可以将代码片段复制并粘贴到一个新的单一视图应用程序中,以查看它的行为。该问题位于与旋转动画一起执行的动画完成块中的某个位置。
import UIKit
let sizeConstant: CGFloat = 60
class ViewController: UIViewController {
    let topView = UIView()
    let backgroundView = UIView()
    let stackView = UIStackView()
    let lLayoutGuide = UILayoutGuide()
    let bLayoutGuide = UILayoutGuide()
    var bottomConstraints = [NSLayoutConstraint]()
    var leftConstraints = [NSLayoutConstraint]()
    var bLayoutHeightConstraint: NSLayoutConstraint!
    var lLayoutWidthConstraint: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
        print(UIScreen.main.bounds)
        //        self.view.layer.masksToBounds = true
        let views = [
            UIButton(type: .infoDark),
            UIButton(type: .contactAdd),
            UIButton(type: .detailDisclosure)
        ]
        views.forEach(self.stackView.addArrangedSubview)
        self.backgroundView.backgroundColor = UIColor.red
        self.backgroundView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(self.backgroundView)
        self.topView.backgroundColor = UIColor.green
        self.topView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(self.topView)
        self.stackView.axis = isPortrait() ? .horizontal : .vertical
        self.stackView.distribution = .fillEqually
        self.stackView.translatesAutoresizingMaskIntoConstraints = false
        self.backgroundView.addSubview(self.stackView)
        self.topView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true
        self.topView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.topView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.topView.heightAnchor.constraint(equalToConstant: 46).isActive = true
        self.view.addLayoutGuide(self.lLayoutGuide)
        self.view.addLayoutGuide(self.bLayoutGuide)
        self.bLayoutGuide.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        self.bLayoutGuide.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.bLayoutGuide.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.bLayoutHeightConstraint = self.bLayoutGuide.heightAnchor.constraint(equalToConstant: isPortrait() ? sizeConstant : 0)
        self.bLayoutHeightConstraint.isActive = true
        self.lLayoutGuide.topAnchor.constraint(equalTo: self.topView.bottomAnchor).isActive = true
        self.lLayoutGuide.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        self.lLayoutGuide.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.lLayoutWidthConstraint = self.lLayoutGuide.widthAnchor.constraint(equalToConstant: isPortrait() ? 0 : sizeConstant)
        self.lLayoutWidthConstraint.isActive = true
        self.stackView.topAnchor.constraint(equalTo: self.backgroundView.topAnchor).isActive = true
        self.stackView.bottomAnchor.constraint(equalTo: self.backgroundView.bottomAnchor).isActive = true
        self.stackView.leadingAnchor.constraint(equalTo: self.backgroundView.leadingAnchor).isActive = true
        self.stackView.trailingAnchor.constraint(equalTo: self.backgroundView.trailingAnchor).isActive = true
        self.bottomConstraints = [
            self.backgroundView.topAnchor.constraint(equalTo: self.bLayoutGuide.topAnchor),
            self.backgroundView.leadingAnchor.constraint(equalTo: self.bLayoutGuide.leadingAnchor),
            self.backgroundView.trailingAnchor.constraint(equalTo: self.bLayoutGuide.trailingAnchor),
            self.backgroundView.heightAnchor.constraint(equalToConstant: sizeConstant)
        ]
        self.leftConstraints = [
            self.backgroundView.topAnchor.constraint(equalTo: self.lLayoutGuide.topAnchor),
            self.backgroundView.bottomAnchor.constraint(equalTo: self.lLayoutGuide.bottomAnchor),
            self.backgroundView.trailingAnchor.constraint(equalTo: self.lLayoutGuide.trailingAnchor),
            self.backgroundView.widthAnchor.constraint(equalToConstant: sizeConstant)
        ]
        if isPortrait() {
            NSLayoutConstraint.activate(self.bottomConstraints)
        } else {
            NSLayoutConstraint.activate(self.leftConstraints)
        }
    }
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        let willBePortrait = size.width < size.height
        coordinator.animate(alongsideTransition: {
            context in
            let halfDuration = context.transitionDuration / 2.0
            UIView.animate(withDuration: halfDuration, delay: 0, options: .overrideInheritedDuration, animations: {
                self.bLayoutHeightConstraint.constant = 0
                self.lLayoutWidthConstraint.constant = 0
                self.view.layoutIfNeeded()
            }, completion: {
                _ in
                // HERE IS THE ISSUE!
                // Putting this inside `performWithoutAnimation` did not helped
                if willBePortrait {
                    self.stackView.axis = .horizontal
                    NSLayoutConstraint.deactivate(self.leftConstraints)
                    NSLayoutConstraint.activate(self.bottomConstraints)
                } else {
                    self.stackView.axis = .vertical
                    NSLayoutConstraint.deactivate(self.bottomConstraints)
                    NSLayoutConstraint.activate(self.leftConstraints)
                }
                self.view.layoutIfNeeded()
                UIView.animate(withDuration: halfDuration) {
                    if willBePortrait {
                        self.bLayoutHeightConstraint.constant = sizeConstant
                    } else {
                        self.lLayoutWidthConstraint.constant = sizeConstant
                    }
                    self.view.layoutIfNeeded()
                }
            })
        })
        super.viewWillTransition(to: size, with: coordinator)
    }
    func isPortrait() -> Bool {
        let size = UIScreen.main.bounds.size
        return size.width < size.height
    }
}以下是一些我无法解决的问题的截图。仔细看看角落:




我假设在重新激活不同的约束数组并强制重新计算后,视图将立即与布局指南相关联,但如图所示,它不会。此外,我不明白为什么红色视图与堆栈视图不同步,即使堆栈视图应该始终遵循superview,这就是红色视图。
PS:测试它的最好方法是iPhone X Plus模拟器。
发布于 2016-12-22 20:34:17
我假设在重新激活不同的约束数组并强制重新计算后,视图将立即与布局指南连接,但如图所示,它不会。
之所以不会发生这种情况,是因为您正在激活/解除coordinator.animate(alongsideTransition:completion:)方法中的约束。当您使用此方法时,动画块中的所有内容都将与视图控制器的转换一起被动画化,因此不会有对布局指南的即时快照。如果您想让红色视图和绿色视图在旋转动画之前立即切换到它们的新位置,那么在视图控制器正在动画时,动画来填充它们想要的位置,那么您可以这样做:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    let willBePortrait = size.width < size.height
    self.bLayoutHeightConstraint.constant = 0
    self.lLayoutWidthConstraint.constant = 0
    if willBePortrait {
        self.stackView.axis = .horizontal
        NSLayoutConstraint.deactivate(self.leftConstraints)
        NSLayoutConstraint.activate(self.bottomConstraints)
    } else {
        self.stackView.axis = .vertical
        NSLayoutConstraint.deactivate(self.bottomConstraints)
        NSLayoutConstraint.activate(self.leftConstraints)
    }
    self.view.layoutIfNeeded()
    coordinator.animate(alongsideTransition: { context in
        let halfDuration = context.transitionDuration / 2
        UIView.animate(withDuration: halfDuration, delay: halfDuration, animations: {
            if willBePortrait {
                self.bLayoutHeightConstraint.constant = sizeConstant
            } else {
                self.lLayoutWidthConstraint.constant = sizeConstant
            }
            self.view.layoutIfNeeded()
        })
    })
    super.viewWillTransition(to: size, with: coordinator)
}编辑:如果您想在转换过程中将红色视图动画化,然后在最后将红色视图动画化,那么这是使用协调器的好时机,因为动画输出可以发生在动画块中,并且可以在其中分割出-块和内块:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    let willBePortrait = size.width < size.height
    if willBePortrait {
        self.stackView.axis = .horizontal
        NSLayoutConstraint.deactivate(self.leftConstraints)
        NSLayoutConstraint.activate(self.bottomConstraints)
    } else {
        self.stackView.axis = .vertical
        NSLayoutConstraint.deactivate(self.bottomConstraints)
        NSLayoutConstraint.activate(self.leftConstraints)
    }
    self.view.layoutIfNeeded()
    coordinator.animate(alongsideTransition: { context in
        let halfDuration = context.transitionDuration / 2
        UIView.animate(withDuration: halfDuration, animations: {
            if willBePortrait {
                self.lLayoutWidthConstraint.constant = 0
            } else {
                self.bLayoutHeightConstraint.constant = 0
            }
        })
        UIView.animate(withDuration: halfDuration, delay: halfDuration, animations: {
            if willBePortrait {
                self.bLayoutHeightConstraint.constant = sizeConstant
            } else {
                self.lLayoutWidthConstraint.constant = sizeConstant
            }
            self.view.layoutIfNeeded()
        })
    })
    super.viewWillTransition(to: size, with: coordinator)
}https://stackoverflow.com/questions/41198069
复制相似问题