旋转设备时崩溃布局

Posted

技术标签:

【中文标题】旋转设备时崩溃布局【英文标题】:Crash layout while rotate device 【发布时间】:2018-06-07 13:56:26 【问题描述】:

我将尝试在设备旋转时停用约束,但我遇到 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】:

切换激活会使崩溃改为考虑

self.v1HeightConstraint.constant = land ? 200 : 0

【讨论】:

以上是关于旋转设备时崩溃布局的主要内容,如果未能解决你的问题,请参考以下文章

设备旋转时是不是有动画自动布局(约束)?

Android Studio 在设备旋转时切换布局

ScrollView 自动布局约束打破设备旋转

Xcode 自动布局和旋转

在旋转具有不同方向布局的设备时继续播放 YouTube 播放器

自动布局:UIImageView 不随设备方向旋转