为什么 StackView 中的视图约束不起作用?
Why view constraints in StackView don’t work?
我正在尝试根据 official doc
实现自定义控件
问题是放置在 HorizontalStackVIew 中的 UIButton 对象填充了它的所有 space 忽略了它的按钮约束(宽度=4.0 高度=4.0)。 (我试过 VerticalStackVIew 和 UITextView 等,但都是一样的)
class MyControl: UIStackView {
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
private func setupButtons() {
// Create the button
let button = UIButton()
button.backgroundColor = UIColor.red
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 4.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 4.0).isActive = true
// Add the button to the stack
addArrangedSubview(button)
}
我有这个日志,但不知道如何处理它:
2017-02-10 16:59:31.900999 MyControl[1835:91456] [LayoutConstraints] 无法同时满足约束。
可能至少以下列表中的约束之一是您不想要的。
试试这个:
(1)查看每个约束并尝试找出您不期望的;
(2) 找到添加了不需要的约束或约束的代码并修复它。
(
"",
"",
"",
“”
)
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。
中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。
2017-02-10 16:59:31.950461 MyControl[1835:91456] [LayoutConstraints] 无法同时满足约束。
可能至少以下列表中的约束之一是您不想要的。
试试这个:
(1)查看每个约束并尝试找出您不期望的;
(2) 找到添加了不需要的约束或约束的代码并修复它。
(
"",
"",
"",
“”
)
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。
中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。
Xcode 8.2.1
你可以试试这个:
class CustomStackView:UIStackView{
override init(frame:CGRect){
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setUpButton()
}
func setUpButton(){
let button:UIButton = UIButton(type: .custom)
button.backgroundColor = .red
self.distribution = .fill
self.addArrangedSubview(button)
}
}
我成功地创建了 UIStackView 对象并仅以编程方式将视图(按钮)放入其中(通过从对象库拖放到故事板上创建它失败)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Button", for: .normal)
button.backgroundColor = .red
stackView.addArrangedSubview(button)
view?.addSubview(stackView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我正在尝试根据 official doc
实现自定义控件问题是放置在 HorizontalStackVIew 中的 UIButton 对象填充了它的所有 space 忽略了它的按钮约束(宽度=4.0 高度=4.0)。 (我试过 VerticalStackVIew 和 UITextView 等,但都是一样的)
class MyControl: UIStackView {
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
private func setupButtons() {
// Create the button
let button = UIButton()
button.backgroundColor = UIColor.red
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 4.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 4.0).isActive = true
// Add the button to the stack
addArrangedSubview(button)
}
我有这个日志,但不知道如何处理它:
2017-02-10 16:59:31.900999 MyControl[1835:91456] [LayoutConstraints] 无法同时满足约束。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 ( "", "", "", “” )
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。 2017-02-10 16:59:31.950461 MyControl[1835:91456] [LayoutConstraints] 无法同时满足约束。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 ( "", "", "", “” )
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。
Xcode 8.2.1
你可以试试这个:
class CustomStackView:UIStackView{
override init(frame:CGRect){
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setUpButton()
}
func setUpButton(){
let button:UIButton = UIButton(type: .custom)
button.backgroundColor = .red
self.distribution = .fill
self.addArrangedSubview(button)
}
}
我成功地创建了 UIStackView 对象并仅以编程方式将视图(按钮)放入其中(通过从对象库拖放到故事板上创建它失败)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Button", for: .normal)
button.backgroundColor = .red
stackView.addArrangedSubview(button)
view?.addSubview(stackView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}