簡體   English   中英

無法同時滿足約束-LayoutConstraints

[英]Unable to simultaneously satisfy constraints - LayoutConstraints

我在TestViewController.swift文件中的代碼后面的代碼中創建了一個按鈕( myTestButton ),正如您在源代碼中看到的那樣,我在文章的底部共享了該按鈕。 旋轉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
    }
}
}

您需要創建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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM