约束未能添加填充

Posted

技术标签:

【中文标题】约束未能添加填充【英文标题】:Constraints failing to add padding 【发布时间】:2018-10-24 08:46:42 【问题描述】:

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

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

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")



【问题讨论】:

检查这是否有帮助 - ***.com/a/51395076/7734643 你的填充值是错误的。 谢谢!我该去睡觉了... ???? 你应该 ????,发生在我们最好的人身上! ?????? 【参考方案1】:

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

【讨论】:

以上是关于约束未能添加填充的主要内容,如果未能解决你的问题,请参考以下文章

为啥填充数据表时缺少唯一约束?

联接 SQL 查询未能填充下拉列表

以编程方式填充 UIStackView

调整图像大小并填充短边

自动布局添加额外的填充

带有自动布局的 uilabel 使用填充调整大小