0

I'm working on a custom control that will eventually be a button. I've successfully built out the design in the interface builder, but when I'm trying to programmatically build the same view, it's not working as expected. I'm attempting to force some padding by setting constants on the constraints that join the labels to their superviews, however, that's not working as expected.

The top button is what I'm expecting (and what I've created in interface builder, do ignore the font differences). The bottom is what I'm getting. There are no errors/warnings being thrown but it's not resizing the superview.

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

}
Jordan Soltman
  • 3,795
  • 17
  • 31

1 Answers1

0

To define padding you could use the layoutMargins property of the containing view. You can refer to this answer: Adding padding to an UIView.

yageek
  • 4,115
  • 3
  • 30
  • 48