How do I change the width and length of the button while the phone screen is in portrait or landscape mode?
I want this to be the button size when the screen is Portrait :
View_Mor.heightAnchor.constraint(equalToConstant: 140).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 140).isActive = true
and when the screen is landscape :
View_Mor.heightAnchor.constraint(equalToConstant: 30).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 30).isActive = true
and this is my code :
class CHATViewController: UIViewController {
@IBOutlet weak var View_Mor: UIView!
override func viewDidLoad() {
super.viewDidLoad()
View_Mor.translatesAutoresizingMaskIntoConstraints = false
View_Mor.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
View_Mor.centerYAnchor.constraint(lessThanOrEqualTo: view.centerYAnchor).isActive = true
View_Mor.heightAnchor.constraint(equalToConstant: 140).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 140).isActive = true
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
View_Mor.heightAnchor.constraint(equalToConstant: 30).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 30).isActive = true
self.view.layoutIfNeeded()
} else {
print("Portrait")
View_Mor.heightAnchor.constraint(equalToConstant: 140).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 140).isActive = true
self.view.layoutIfNeeded()
}
}
}
But it doesn't work