简体   繁体   English

无法同时满足约束-LayoutConstraints

[英]Unable to simultaneously satisfy constraints - LayoutConstraints

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. 我在TestViewController.swift文件中的代码后面的代码中创建了一个按钮( myTestButton ),正如您在源代码中看到的那样,我在文章的底部共享了该按钮。 I am getting below error after i rotate the Iphone(scope 2). 旋转Iphone(范围2)后,我收到以下错误消息。 Why am I getting this error? 为什么会出现此错误? How can I fix this problem? 我该如何解决这个问题?

The error in output window in xcode: 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 您需要创建2个数组

 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 然后玩activate / deactivate

NSLayoutConstraint.deactivate(firstCons)
NSLayoutConstraint.activate(secondCons)

according to current orientation , BTW you can exclude centerX & heightAnchor constraints for similarity 根据当前方向,顺便说一句,您可以排除centerX和heightAnchor约束以实现相似性

myTestButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
myTestButton.heightAnchor.constraint(equalToConstant: 120).isActive = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM