设置按钮宽度以适合动态按钮标题

时间:2019-01-29 04:03:23

标签: ios uibutton constraints

我的用户界面结构为1级(上),2级(下)和一些控件 在1级中,我有一个标签L1 在2级中,我有一个按钮,标签为L2

在2级中,我的按钮可能会在运行时删除,我希望我的标签L2与L1的前沿对齐

我在这里面临两个问题

  1. 以编程方式设置按钮标题时,我希望将按钮设置为当文本增加时其宽度增大,而当文本内容较少时减小其宽度。这没有发生。请在下面的屏幕中查看我已经设置的约束

  2. 从超级视图中删除按钮时,我希望我的L2标签Leading与L1 Leading对齐。因此,我从L2.leading = L1.leading创建约束,优先级为999

在这种情况下,即使我在其中输入文字,按钮的尺寸也会减小到几乎为0。请建议我进行设置

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

问题1:

.horizontal UIStackview用于按钮和文本。将其分布设置为.fill。对于按钮,将.required的contentCompression优先级设置为.horizontal,并将.required的contenHugging优先级设置为.horizontal。因此,无论如何,Button都将始终包裹文本。

问题2:

当放置在stackview中时,不必从超级视图中删除按钮。只需使用isHidden隐藏它即可。

代码演示

class SampleVC: UIViewController {

private var didAddConstraint = false

// Basic Views
private let label: UILabel = {
    let view = UILabel()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.text = "Label"
    return view
}()
private let topButton: UIButton = {
    let view = UIButton()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.setTitle("Button", for: .normal)
    view.setTitleColor(.gray, for: .highlighted)
    view.backgroundColor = .green

    view.setContentHuggingPriority(.required, for: .horizontal)
    view.setContentCompressionResistancePriority(.required, for: .horizontal)

    return view
}()
private let rightLabel: UILabel = {
    let view = UILabel()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.numberOfLines = 0
    view.text = "label"
    view.backgroundColor = .red
    return view
}()
private lazy var stackview: UIStackView = {
    let view = UIStackView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.axis = .horizontal
    view.distribution = .fill

    view.addArrangedSubview(topButton)
    view.addArrangedSubview(rightLabel)
    return view
}()


override func loadView() {
    super.loadView()

    view.addSubview(label)
    view.addSubview(stackview)

    view.setNeedsUpdateConstraints()

    view.backgroundColor = .white
}
override func updateViewConstraints() {
    super.updateViewConstraints()

    if didAddConstraint == false {
        didAddConstraint = true

        // top label
        label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16.0).isActive = true
        label.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
        label.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

        // stackview
        stackview.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16.0).isActive = true
        stackview.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 8.0).isActive = true
        stackview.rightAnchor.constraint(equalToSystemSpacingAfter: view.rightAnchor, multiplier: 16.0).isActive = true
    }
}



override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

        // TEST Code
        // topButton.setTitle("TEST TEST TEST", for: .normal)
        // topButton.isHidden = true

    }
}