I have a stack view that contains a title label and a view which happens to be another stack view that I want to add custom spacing to. The composition I am using is based on the way that is described in the documentation for laying out views in UIStackView in the Define the stack’s size along its axis section. The stack has a vertical axis and its top and bottom edges are pinned to the content view's top and bottom edges (the stack is a subview of a UICollectionViewCell). The leading edge of the stack is pinned to the leading edge of the content view and is offset by a constant 24 points.
My actual implementation is as follows:
func setUp() {
titleView = UILabel()
femaleIcon = UIImageView()
maleIcon = UIImageView()
femaleIcon.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(switchFemaleIcon)))
femaleIcon.contentMode = .scaleAspectFit
maleIcon.contentMode = .scaleAspectFit
maleIcon.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(switchMaleIcon)))
let iconStackView = UIStackView(arrangedSubviews: [
femaleIcon,
maleIcon
])
iconStackView.axis = .horizontal
iconStackView.translatesAutoresizingMaskIntoConstraints = false
iconStackView.setCustomSpacing(50, after: femaleIcon)
iconStackView.backgroundColor = .yellow
let stackView = UIStackView(arrangedSubviews: [
titleView,
iconStackView
])
stackView.axis = .vertical
stackView.alignment = .leading
contentView.addSubview(stackView)
stackView.backgroundColor = .magenta
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.setCustomSpacing(14, after: titleView)
NSLayoutConstraint.activate([
femaleIcon.widthAnchor.constraint(equalToConstant: 30),
femaleIcon.heightAnchor.constraint(equalToConstant: 48),
maleIcon.widthAnchor.constraint(equalToConstant: 36),
maleIcon.heightAnchor.constraint(equalToConstant: 36.5),
stackView.topAnchor.constraint(equalTo: contentView.topAnchor),
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
])
}
The representation I want to achieve is as follows:
iconStackView
to leading oftitleView
is 24?iconStackView
to be aligned with the center ofcontentView
. I updated the image of what I want to better reflect my intent.titleView
andiconStackView
fromstackView
? Just use normal constraintstackView
?