我尝试删除约束。我想对肖像和风景有不同的限制。如果我将方向更改为纵向,则调用函数 setupConstrainsInPortrait ,反之亦然。我有两个功能。
此功能设定纵向模式。
func setupConstrainsInPortrait() {
view.addSubview(myView)
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
myView.heightAnchor.constraint(equalToConstant: 300).isActive = true
myView.widthAnchor.constraint(equalToConstant: view.frame.size.width).isActive = true
view.addSubview(switchKmM)
switchKmM.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
switchKmM.topAnchor.constraint(equalTo: myView.bottomAnchor, constant: 10).isActive = true
switchKmM.heightAnchor.constraint(equalToConstant: 50).isActive = true
switchKmM.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
view.addSubview(speedLbl)
speedLbl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
speedLbl.topAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
speedLbl.heightAnchor.constraint(equalToConstant: 50).isActive = true
speedLbl.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}
此功能设置横向模式。
func setupConstrainsInLandScape() {
view.addSubview(myView)
myView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
myView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
myView.heightAnchor.constraint(equalToConstant: 300).isActive = true
myView.widthAnchor.constraint(equalToConstant: 150).isActive = true
view.addSubview(switchKmM)
switchKmM.leftAnchor.constraint(equalTo: myView.rightAnchor, constant: 30).isActive = true
switchKmM.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
switchKmM.heightAnchor.constraint(equalToConstant: 50).isActive = true
switchKmM.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}
我在viewWillTransition
中使用这些函数override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
print("landscape!")
view.backgroundColor = .green
setupConstrainsInLandScape()
self.viewWillLayoutSubviews()
}
else {
print("portrét")
view.backgroundColor = .white
setupConstrainsInPortrait()
self.viewWillLayoutSubviews()
}
super.viewWillTransition(to: size, with: coordinator)
}
问题是,横向模式中的约束(来自肖像功能)不会被删除
我希望有人可以帮助我...谢谢
答案 0 :(得分:3)
你做得不够好。您需要保留对所激活的所有约束的引用,以便稍后可以停用它们。例如,您说:
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
myView.heightAnchor.constraint(equalToConstant: 300).isActive = true
myView.widthAnchor.constraint(equalToConstant: view.frame.size.width).isActive = true
相反,你需要说出类似
的内容self.myConstraints1 = [
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
myView.heightAnchor.constraint(equalToConstant: 300),
myView.widthAnchor.constraint(equalToConstant: view.frame.size.width),
]
现在激活所有这些约束。始终以同样的方式进行。
因此,当由于方向发生变化而停止约束时,您可以参考它们并且可以这样做。
答案 1 :(得分:-3)
您可以使用以下方法删除约束:
myView.constraints.removeAll()
在设置新的之前。