ios - 约束无法添加填充

标签 ios swift uiview uikit nslayoutconstraint

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

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

enter image description here

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

}

最佳答案

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

关于ios - 约束无法添加填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964613/

相关文章:

ios - 如何从本地 Realm 数据库恢复数据?

iphone - 有模拟时钟的 iPhone 代码示例吗?

ios - 一类 UIView 也可以是 UITextFieldDelegate 吗?

ios - ACAccountStore 请求AccessToAccountsWithType :options:completion:accountStoreHandler returns granted = NO immediately

javascript - Web Audio API 演示在 iOS 上不起作用

ios - 放置在 nib 文件中时如何隐藏 UIPickerView

ios - swift ios9 : if tab already selected, 单击当前选项卡时将其切换到另一个选项卡

ios - popViewController(动画 : true) animation works slow

ios - 裁剪图像中心

iphone - 如何正确地从 Three20 库中删除组件