Reputation: 338
Basically, I have a tableview and when I switch to landscape I want it to set the table view to fullscreen and when I switch it back to portrait, it should revert to default constraints. It works if I remove the breakpoint.
However, when I switch it back from landscape to portrait, it does not return to default. When I switch it back to portrait, I am editing the constraints for it to do what I want. However, I am open to other solutions. Please advise me on what I should do. I added the breakpoint using UIViewAlertForUnsatisfiableConstraints
just like this image:
override func viewDidLoad() {
super.viewDidLoad()
parseData(noOfPosts:90)
TableView.delegate = self
TableView.dataSource = self
TableView.tableFooterView = UIView()
let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)
myTop2Constraint.isActive = true
myBtm2Constraint.isActive = true
if(UIDevice.current.orientation.isLandscape){
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false
}
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)
let myTopConstraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
let myBtmConstraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
myTopConstraint.isActive = false
myBtmConstraint.isActive = false
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false
if(UIDevice.current.orientation.isLandscape){
self.catIsuSemasa.isHidden = true
self.catOthers.isHidden = true
self.catSocial.isHidden = true
self.catPolitics.isHidden = true
if(myTop2Constraint.isActive){
print("error1?")
myTop2Constraint.isActive = false
print("error2?")
myBtm2Constraint.isActive = false
print("error3?")
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}else{
TableView.removeAllConstraints()
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}
}else{
self.catIsuSemasa.isHidden = false
self.catOthers.isHidden = false
self.catSocial.isHidden = false
self.catPolitics.isHidden = false
if(myTopConstraint.isActive){
print("error")
myTopConstraint.isActive = false
print("error2")
myBtmConstraint.isActive = false
print("error3")
myTop2Constraint.isActive = true
print("error4")
myBtm2Constraint.isActive = true
print("run?")
}
}
}
extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}
Upvotes: 1
Views: 453
Reputation: 539
App support landscape and portrait then we do not required set constrain programatically we can achieve same thing by using storyboard's size class (vary for traits). By using size class we can create adaptive layout design. we can set different constrain for landscape and portrait app.
Please check below tutorial link :
Upvotes: 1