首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >约束添加填充失败

约束添加填充失败
EN

Stack Overflow用户
提问于 2018-10-24 16:46:43
回答 1查看 618关注 0票数 0

我正在开发一个自定义控件,它最终会变成一个按钮。我已经成功地在界面构建器中构建了设计,但是当我试图以编程方式构建相同的视图时,它并没有像预期的那样工作。我试图通过在将标签连接到它们的超视图的约束上设置常量来强制一些填充,然而,这并不像预期的那样工作。

顶部的按钮是我所期望的(以及我在界面构建器中创建的,请忽略字体差异)。底部就是我要得到的。没有抛出错误/警告,但它没有调整superview的大小。

代码语言:javascript
复制
class RoundedButton3: UIView {

var stringLabel: UILabel
var numberLabel: UILabel
var leftBackground: UIView
var rightBackground: UIView
var stackView: UIStackView

override init(frame: CGRect) {
    stringLabel = UILabel()
    numberLabel = UILabel()
    leftBackground = UIView()
    rightBackground = UIView()
    stackView = UIStackView()
    super.init(frame: frame)

    self.contentMode = .scaleToFill
    self.autoresizingMask = [.flexibleBottomMargin, .flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin]

    self.translatesAutoresizingMaskIntoConstraints = false

    stringLabel.translatesAutoresizingMaskIntoConstraints = false
    numberLabel.translatesAutoresizingMaskIntoConstraints = false
    leftBackground.translatesAutoresizingMaskIntoConstraints = false
    rightBackground.translatesAutoresizingMaskIntoConstraints = false
    stackView.translatesAutoresizingMaskIntoConstraints = false


    self.addSubview(stackView)
    leftBackground.addSubview(stringLabel)
    rightBackground.addSubview(numberLabel)
    stackView.addArrangedSubview(leftBackground)
    stackView.addArrangedSubview(rightBackground)

    stackView.axis = .horizontal
    stackView.alignment = .fill
    stackView.distribution = .fill

    stringLabel.setContentHuggingPriority(UILayoutPriorityRequired, for: .horizontal)
    stringLabel.setContentHuggingPriority(UILayoutPriorityRequired, for: .vertical)
    stringLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .horizontal)
    stringLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .vertical)
    numberLabel.setContentHuggingPriority(UILayoutPriorityRequired, for: .horizontal)
    numberLabel.setContentHuggingPriority(UILayoutPriorityRequired, for: .vertical)
    numberLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .horizontal)
    numberLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .vertical)

    self.topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true
    self.bottomAnchor.constraint(equalTo: stackView.bottomAnchor).isActive = true
    self.rightAnchor.constraint(equalTo: stackView.rightAnchor).isActive = true
    self.leftAnchor.constraint(equalTo: stackView.leftAnchor).isActive = true

    leftBackground.leftAnchor.constraint(equalTo: stringLabel.leftAnchor, constant: 4).isActive = true
    leftBackground.rightAnchor.constraint(equalTo: stringLabel.rightAnchor, constant: 4).isActive = true
    leftBackground.topAnchor.constraint(equalTo: stringLabel.topAnchor, constant: 4).isActive = true
    leftBackground.bottomAnchor.constraint(equalTo: stringLabel.bottomAnchor, constant: 4).isActive = true

    rightBackground.leftAnchor.constraint(equalTo: numberLabel.leftAnchor, constant: 4).isActive = true
    rightBackground.rightAnchor.constraint(equalTo: numberLabel.rightAnchor, constant: 4).isActive = true
    rightBackground.topAnchor.constraint(equalTo: numberLabel.topAnchor, constant: 4).isActive = true
    rightBackground.bottomAnchor.constraint(equalTo: numberLabel.bottomAnchor, constant: 4).isActive = true

    stringLabel.text = "RESERVE"
    numberLabel.text = "10"

    stringLabel.textColor = AppColors.WSF_GREEN
    numberLabel.textColor = UIColor.white
    rightBackground.backgroundColor = AppColors.WSF_GREEN
    leftBackground.backgroundColor = UIColor.white
    self.layer.borderColor = AppColors.WSF_GREEN.cgColor
    self.layer.borderWidth = 3
    self.layer.cornerRadius = 4
    self.layer.masksToBounds = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}
EN

回答 1

Stack Overflow用户

发布于 2018-10-24 18:08:11

要定义填充,可以使用包含视图的layoutMargins属性。你可以参考这个答案:Adding padding to an UIView

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52964613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档