Я попытаюсь деактивировать ограничение во время вращения устройства, но получаю сбой макета 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)>