在 swift 中为按钮添加约束(Apple iOS 教程)

Posted

技术标签:

【中文标题】在 swift 中为按钮添加约束(Apple iOS 教程)【英文标题】:Add constraints to button in swift (Apple iOS Tutorial) 【发布时间】:2016-12-24 21:04:40 【问题描述】:

我正在为 ios 开发编写 Apples Tutorial(章节:“将按钮添加到视图”)。只需复制以下代码,我就会得到不同的结果:

import UIKit

@IBDesignable class RatingControl: UIStackView 

//MARK: Properties
private var ratingButtons = [UIButton]()

var rating = 0
@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0)
@IBInspectable var starCount: Int = 5

//MARK: Initialisation
override init(frame: CGRect) 
    super.init(frame: frame)
    setupButtons()

required init(coder: NSCoder) 
    super.init(coder: coder)
    setupButtons()


//MARK: Private Methods
private func setupButtons() 

        // Create the button
        let button = UIButton()
        button.backgroundColor = UIColor.red

        // Add constraints
        button.translatesAutoresizingMaskIntoConstraints = false
        button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
        button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true

        // Setup the button action
        button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)

        // Add the button to the stack
        addArrangedSubview(button)

        // Add the new button to the rating button array
        ratingButtons.append(button)


//MARK: Button Action
func ratingButtonTapped(button: UIButton) 
    print("Button pressed ????")

约束似乎不起作用。我的红色按钮的大小与其超级堆栈视图完全相同。它不限于 44x44。

控制台报告不能同时满足所有约束:

      2016-12-25 18:43:02.375251 FoodTracker[13644:1695258] [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:0x600000092160 UIButton:0x7ff15f40a740.width == 44   (active)>",
    "<NSLayoutConstraint:0x608000095a90 'UISV-canvas-connection' FoodTracker.RatingControl:0x7ff15f6116c0.leading == UIButton:0x7ff15f40a740.leading   (active)>",
    "<NSLayoutConstraint:0x608000095b30 'UISV-canvas-connection' H:[UIButton:0x7ff15f40a740]-(0)-|   (active, names: '|':FoodTracker.RatingControl:0x7ff15f6116c0 )>",
    "<NSLayoutConstraint:0x608000095630 'UIView-Encapsulated-Layout-Width' FoodTracker.RatingControl:0x7ff15f6116c0.width == 200   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000092160 UIButton:0x7ff15f40a740.width == 44   (active)>

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.
2016-12-25 18:43:02.376266 FoodTracker[13644:1695258] [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:0x600000091e90 UIButton:0x7ff15f40a740.height == 44   (active)>",
    "<NSLayoutConstraint:0x608000095b80 'UISV-canvas-connection' FoodTracker.RatingControl:0x7ff15f6116c0.top == UIButton:0x7ff15f40a740.top   (active)>",
    "<NSLayoutConstraint:0x608000095c70 'UISV-canvas-connection' V:[UIButton:0x7ff15f40a740]-(0)-|   (active, names: '|':FoodTracker.RatingControl:0x7ff15f6116c0 )>",
    "<NSLayoutConstraint:0x608000095680 'UIView-Encapsulated-Layout-Height' FoodTracker.RatingControl:0x7ff15f6116c0.height == 110   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000091e90 UIButton:0x7ff15f40a740.height == 44   (active)>

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.

但是,我没有为堆栈视图定义其他约束,除了上面类中的约束。同样在 Interface Builder 中,大纲中的堆栈视图没有其他约束。 'UISV-canvas-connection' 来自哪里?

【问题讨论】:

为什么不在storyboard或xib中定义这个? 我只是想像教程一样尝试,但不知道为什么会发生不同的事情。 你能显示你的整个按钮代码吗?你是否使用调试器? 我还没有真正使用过。两天前我才开始开发 iOS。我刚刚添加了控制台输出,它说要添加一个符号断点。但是我得到的指针地址并没有告诉我太多。 谢谢!我发现我做的不同:我没有将水平堆栈视图添加到垂直 SV,而是在下面。现在,当我纠正它时,一切正常。但我仍然不明白额外的约束来自哪里以及如何禁用它们。如果我没有在我的代码中加入约束,我在哪里可以找到它们? 【参考方案1】:

在使用 Apple 的教程时发现了同样的问题,并且确实犯了在 Vertical Stack View 之外创建 Horizo​​ntal Stack View 的错误。

将它拖回垂直堆栈视图后,它对我有用:)

【讨论】:

你能解释一下为什么吗? 我也很想了解为什么这会有所作为 完美。我自己也在寻找解决方案。很高兴有人已经发现了。在手册中它告诉In your storyboard, use the Object library to find a Horizontal Stack View object, and drag one into your storyboard scene so that it’s "in the stack view" below the image view.

以上是关于在 swift 中为按钮添加约束(Apple iOS 教程)的主要内容,如果未能解决你的问题,请参考以下文章