I have created a button ( myTestButton
) in codebehind in TestViewController.swift
file as you see in the source code that I have shared bottom of the post. I am getting below error after i rotate the Iphone(scope 2). Why am I getting this error? How can I fix this problem?
The error in output window in xcode:
device has rotated! is landscape mode?: true
scope: 2
2019-07-14 15:23:14.411875+0300 myproject[12522:4008763] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x28029b8e0 UIButton:0x103215110.centerY == UIView:0x103204e10.centerY (active)>",
"<NSLayoutConstraint:0x28028c550 UIButton:0x103215110.centerY == UIView:0x103204e10.centerY + 60 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x28028c550 UIButton:0x103215110.centerY == UIView:0x103204e10.centerY + 60 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-07-14 15:23:14.413028+0300 myproject[12522:4008763] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x28029ba70 UIButton:0x103215110.width == UIView:0x103204e10.width - 20 (active)>",
"<NSLayoutConstraint:0x28028c9b0 UIButton:0x103215110.width == UIView:0x103204e10.width - 40 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x28029ba70 UIButton:0x103215110.width == UIView:0x103204e10.width - 20 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Here is the source code:
import UIKit
class TestViewController: UIViewController {
var isOrientationStatusLandscape : Bool = false
let myTestButton : UIButton =
{
let mytestbutton = UIButton()
mytestbutton.backgroundColor = UIColor.yellow
mytestbutton.translatesAutoresizingMaskIntoConstraints = false
return mytestbutton
}()
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
if self.isOrientationStatusLandscape == true {self.isOrientationStatusLandscape = false} else {self.isOrientationStatusLandscape = true}
print("device has rotated! is landscape mode?: \(self.isOrientationStatusLandscape)")
self.setupView()
}
func setupView()
{
self.prepareTheButton()
}
func prepareTheButton()
{
view.addSubview(myTestButton)
if isOrientationStatusLandscape == false
{
print("scope: 1")
myTestButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
myTestButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myTestButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -20).isActive = true
myTestButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
}
else
{
print("scope: 2")
myTestButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 60).isActive = true
myTestButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myTestButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -40).isActive = true
myTestButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
}
}
}
You need to create 2 arrays
let firstCons = [
myTestButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
myTestButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -20),
]
let secondCons = [
myTestButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 60),
myTestButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -40),
]
Then play with activate
/deactivate
NSLayoutConstraint.deactivate(firstCons)
NSLayoutConstraint.activate(secondCons)
according to current orientation , BTW you can exclude centerX & heightAnchor constraints for similarity
myTestButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myTestButton.heightAnchor.constraint(equalToConstant: 120).isActive = true