Swift UIView子类初始化时的固定高度

Swift UIView子类初始化时的固定高度,swift,uiview,autolayout,initialization,subclass,Swift,Uiview,Autolayout,Initialization,Subclass,我有一个UIButton子类,它的高度必须是80pt,但在UIStackView中使用时,其宽度表现正常。。。如何在子类中实现这一点 以下代码成功更改了高度,但UIStackView不会调整布局以精确调整高度: class MenuSelectionButton: UIButton { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.layer.corn

我有一个UIButton子类,它的高度必须是80pt,但在UIStackView中使用时,其宽度表现正常。。。如何在子类中实现这一点

以下代码成功更改了高度,但UIStackView不会调整布局以精确调整高度:

class MenuSelectionButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.cornerRadius = 5.0
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        var newFrame = frame
        newFrame.size.height = 80
        frame = newFrame
    }

}
工作代码:

class MenuSelectionButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.cornerRadius = 5.0
        addHeightConstraint()
    }

    private func addHeightConstraint () {
        let heightConstraint = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 80)
        NSLayoutConstraint.activateConstraints([heightConstraint])
    }

}

将按钮的高度限制为80,并让“自动布局”处理它

或者,由于您使用的是
UIButton
的自定义子类,因此重写
instrinsicContentSize
以返回80的高度:

import UIKit

@IBDesignable
class MenuSelectionButton: UIButton {

    // Xcode uses this to render the button in the storyboard.
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    // The storyboard loader uses this at runtime.
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override func intrinsicContentSize() -> CGSize {
        return CGSize(width: super.intrinsicContentSize().width, height: 80)
    }

    private func commonInit() {
        self.layer.cornerRadius = 5.0
    }

}

将按钮的高度限制为80,并让“自动布局”处理它

或者,由于您使用的是
UIButton
的自定义子类,因此重写
instrinsicContentSize
以返回80的高度:

import UIKit

@IBDesignable
class MenuSelectionButton: UIButton {

    // Xcode uses this to render the button in the storyboard.
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    // The storyboard loader uses this at runtime.
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override func intrinsicContentSize() -> CGSize {
        return CGSize(width: super.intrinsicContentSize().width, height: 80)
    }

    private func commonInit() {
        self.layer.cornerRadius = 5.0
    }

}

这需要在button子类中完成,以避免需要在按钮的每个实例中手动添加约束-这可能吗?根据您定义的
init(coder:)
,我假设您正在从故事板加载按钮。您可以在故事板中创建约束。按钮没有附带的XIB文件-它是UIButton的一个直接子类。按钮正在添加到其他XIB文件中,可以在其中添加约束,但是,以编程方式添加约束要容易得多,如我的编辑问题中所示。我已经用另一个您可能更喜欢的解决方案更新了我的答案。这需要在button子类中完成,以避免需要手动在按钮的每个实例中添加约束-可能吗?我假设,根据您定义的
init(coder:)
,您正在从故事板加载按钮。您可以在故事板中创建约束。按钮没有附带的XIB文件-它是UIButton的一个直接子类。按钮正在添加到其他XIB文件中,可以在其中添加约束,但以编程方式添加约束要容易得多,如我的编辑问题中所示。我已使用另一种您可能更喜欢的解决方案更新了我的答案。