旋转设备时崩溃布局

时间:2018-06-07 13:56:27

标签: ios swift rotation constraints uistackview

我会尝试在设备旋转时停用约束,但我得到Apple布局崩溃。

这是代码:

class TestViewController: UIViewController {

    let v1: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    var v1HeightConstraint: NSLayoutConstraint!

    let v2: UIView = {
        let view = UIView()
        view.backgroundColor = .blue
        return view
    }()

    lazy var stack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.addArrangedSubview(v1)
        stack.addArrangedSubview(v2)
        return stack
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(stack)

        stack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        stack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        stack.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        stack.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true

        v1HeightConstraint = v1.heightAnchor.constraint(equalToConstant: 200)
        v1HeightConstraint.isActive = true
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: { (_) in
            let land = UIDevice.current.orientation.isLandscape
            self.stack.axis = land ? .horizontal : .vertical
            self.stack.distribution = land ? .fillEqually : .fill
            self.v1HeightConstraint.isActive = !land
        }, completion: nil )
    }
}

在设备旋转时,也许有人知道在视图控制器中删除/更新约束的好方法吗?或者知道位置的生命周期发生了变化?

错误:

"<NSLayoutConstraint:0x60400008a730 UIStackView:0x7feb4330fc90.top == UILayoutGuide:0x6040007a3100'UIViewSafeAreaLayoutGuide'.top   (active)>",
"<NSLayoutConstraint:0x60400008a780 UIStackView:0x7feb4330fc90.bottom == UILayoutGuide:0x6040007a3100'UIViewSafeAreaLayoutGuide'.bottom   (active)>",
"<NSLayoutConstraint:0x60400008a960 UIView:0x7feb4330c7c0.height == 200   (active)>",
"<NSLayoutConstraint:0x60400008a9b0 'UISV-canvas-connection' UIStackView:0x7feb4330fc90.top == UIView:0x7feb4330c7c0.top   (active)>",
"<NSLayoutConstraint:0x60400008b7c0 'UISV-canvas-connection' V:[UIView:0x7feb4330c7c0]-(0)-|   (active, names: '|':UIStackView:0x7feb4330fc90 )>",
"<NSLayoutConstraint:0x608000086310 'UIView-Encapsulated-Layout-Height' UIView:0x7feb43310030.height == 375   (active)>",
"<NSLayoutConstraint:0x604000089c90 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6040007a3100'UIViewSafeAreaLayoutGuide']-(0)-|   (active, names: '|':UIView:0x7feb43310030 )>",
"<NSLayoutConstraint:0x604000089b00 'UIViewSafeAreaLayoutGuide-top' V:|-(0)-[UILayoutGuide:0x6040007a3100'UIViewSafeAreaLayoutGuide']   (active, names: '|':UIView:0x7feb43310030 )>"

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60400008a960 UIView:0x7feb4330c7c0.height == 200   (active)>

1 个答案:

答案 0 :(得分:0)

切换激活使得崩溃反而考虑

self.v1HeightConstraint.constant = land ? 200 : 0