Ios 以编程方式指定约束失败UIViewController Xcode 11

Ios 以编程方式指定约束失败UIViewController Xcode 11,ios,xcode11,Ios,Xcode11,我可能已经做了数百次了,但我已经开始意识到,在Xcode 11上有一个全新的项目,当我尝试在UIViewController上设置约束时,我会遇到约束问题 我的代码: class ViewController: UIViewController { private lazy var button: UIButton = { let button = UIButton() button.setTitle("Click me!", for: .normal

我可能已经做了数百次了,但我已经开始意识到,在Xcode 11上有一个全新的项目,当我尝试在UIViewController上设置约束时,我会遇到约束问题

我的代码:

class ViewController: UIViewController {

    private lazy var button: UIButton = {
        let button = UIButton()

        button.setTitle("Click me!", for: .normal)

        return button
    }()

    init() {
        super.init(nibName: nil, bundle: nil)

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .red
        view.addSubview(button)
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.heightAnchor.constraint(equalToConstant: 52).isActive = true
        button.widthAnchor.constraint(equalToConstant: 128).isActive = true

    }
}
这是从我的场景公使那里传来的

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let windowScene = scene as? UIWindowScene {

        let window = UIWindow(windowScene: windowScene)
        let timeline = ViewController()

        let navigation = UINavigationController(rootViewController: timeline)
        window.rootViewController = navigation

        self.window = window
        window.makeKeyAndVisible()
    }
}
控制台消息:

2020-02-25 10:18:00.250858+0000 TestScene[374:17466] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283144be0 h=--& v=--& UIButton:0x119f199d0'Click me!'.minX == 0   (active, names: '|':UIView:0x119f19860 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x283144c30 h=--& v=--& UIButton:0x119f199d0'Click me!'.width == 0   (active)>",
    "<NSLayoutConstraint:0x283144640 UIButton:0x119f199d0'Click me!'.centerX == UIView:0x119f19860.centerX   (active)>",
    "<NSLayoutConstraint:0x283145310 'UIView-Encapsulated-Layout-Width' UIView:0x119f19860.width == 414   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283144640 UIButton:0x119f199d0'Click me!'.centerX == UIView:0x119f19860.centerX   (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.
2020-02-25 10:18:00.251321+0000 TestScene[374:17466] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x283145270 h=--& v=--& UIButton:0x119f199d0'Click me!'.minY == 0   (active, names: '|':UIView:0x119f19860 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x2831452c0 h=--& v=--& UIButton:0x119f199d0'Click me!'.height == 0   (active)>",
    "<NSLayoutConstraint:0x283144140 UIButton:0x119f199d0'Click me!'.centerY == UIView:0x119f19860.centerY   (active)>",
    "<NSLayoutConstraint:0x283145360 'UIView-Encapsulated-Layout-Height' UIView:0x119f19860.height == 896   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283144140 UIButton:0x119f199d0'Click me!'.centerY == UIView:0x119f19860.centerY   (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.
2020-02-25 10:26:19.319362+0000 TestScene[374:17466] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2020-02-25 10:18:00.250858+0000测试场景[374:17466][布局约束]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-02-25 10:18:00.251321+0000测试场景[374:17466][布局约束]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-02-25 10:26:19.319362+0000测试场景[374:17466]无法结束背景任务:不存在标识符为1(0x1)的背景任务,或者它可能已经结束。中断UIApplicationEndBackgroundTaskError()以进行调试。

我做错了什么?

您没有在
按钮上将
翻译自动调整大小GMaskintoConstraints
设置为
false
private lazy var button: UIButton = {
    let button = UIButton()
    button.setTitle("Click me!", for: .normal)
    button.translatesAutoResizingMaskIntoConstraints = false
    return button
}()