无法同时满足约束-LayoutConstraints

时间:2019-07-14 12:32:33

标签: ios swift nslayoutconstraint

我已经在myTestButton文件中的代码后面的代码中创建了一个按钮(TestViewController.swift,正如您在源代码中看到的那样,我已经分享了该文章的底部。旋转Iphone(范围2)后,我收到以下错误消息。为什么会出现此错误?我该如何解决这个问题?

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.

以下是源代码:

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
    }
}
}

1 个答案:

答案 0 :(得分:0)

您需要创建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), 
 ]

然后玩activate / deactivate

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

根据当前方向,顺便说一下,您可以排除centerX和heightAnchor约束以实现相似性

myTestButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
myTestButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
相关问题