gpt4 book ai didi

ios - 当视觉格式语言仅设置一个约束时,无法同时满足约束

转载 作者:行者123 更新时间:2023-11-30 14:14:23 25 4
gpt4 key购买 nike

我做了一个新项目来练习iOS自动布局功能,视觉格式语言

我只做了一个按钮,如下

var button:UIButton!

我做了一个方法,它将添加按钮到 self.view,名为 initViews 如下

func initViews() -> Void {

button = UIButton()
button.setTitle("Button", forState: .Normal)
button.backgroundColor = UIColor.blueColor()

self.view.addSubview(button)
self.createConstraints()
}

在此之后,我创建了另一个方法,如下所示用于设置约束。

func createConstraints () -> Void {

//Views to add constraints to
let views = Dictionary(dictionaryLiteral: ("button",button))

let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[button]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
self.view.addConstraints(horizontalConstraints)

// let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[button]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)

//self.view.addConstraints(verticalConstraints)
}

现在,我在 ViewDidLoad 中调用 initViews,但是当它执行时,它会在 LOG 中发出警告。

我确定在没有垂直约束的情况下我看不到按钮。但我更注重警告。

警告:

2015-07-12 11:10:06.495 iOSAdvanceAutoLayout Project[1248:37226] 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)
(
"<NSLayoutConstraint:0x7fc9f062ffb0 UIButton:0x7fc9f061ccd0'Button'.leading == UIView:0x7fc9f0572cb0.leadingMargin>",
"<NSLayoutConstraint:0x7fc9f0630180 UIView:0x7fc9f0572cb0.trailingMargin == UIButton:0x7fc9f061ccd0'Button'.trailing>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056a820 h=--& v=--& H:[UIButton:0x7fc9f061ccd0'Button'(0)]>",
"<NSLayoutConstraint:0x7fc9f056d0e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fc9f0572cb0(375)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc9f0630180 UIView:0x7fc9f0572cb0.trailingMargin == UIButton:0x7fc9f061ccd0'Button'.trailing>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-07-12 11:10:06.496 iOSAdvanceAutoLayout Project[1248:37226] 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)
(
"<NSLayoutConstraint:0x7fc9f062ffb0 UIButton:0x7fc9f061ccd0'Button'.leading == UIView:0x7fc9f0572cb0.leadingMargin>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056c210 h=--& v=--& UIButton:0x7fc9f061ccd0'Button'.midX ==>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056a820 h=--& v=--& H:[UIButton:0x7fc9f061ccd0'Button'(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056d880 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x7fc9f0572cb0] (Names: '|':UIWindow:0x7fc9f061b9d0 )>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc9f062ffb0 UIButton:0x7fc9f061ccd0'Button'.leading == UIView:0x7fc9f0572cb0.leadingMargin>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

任何人都可以指导我为什么在只设置一个约束的情况下会出现此警告吗?
谢谢

最佳答案

创建按钮后添加以下行

    button.translatesAutoresizingMaskIntoConstraints = false

还添加垂直约束,按如下方式编辑您的方法。

func createConstraints () -> Void {

//Views to add constraints to
let views = Dictionary(dictionaryLiteral: ("button",button))

let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-30.0-[button]-30.0-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: views)
self.view.addConstraints(horizontalConstraints)



let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-30.0-[button]-30.0-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: views)

self.view.addConstraints(verticalConstraints)
}

关于ios - 当视觉格式语言仅设置一个约束时,无法同时满足约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31364984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com