I have a stackview in my iOS app with many arranged elements. For most of the elements, I want to center them vertically in my stack. However, for some elements, I want to visually tweak them by moving them a few units above or below the center line.
let horizontalStack: UIStackView = {
let stackView = UIStackView().disableAutoresizingMask()
stackView.axis = .horizontal
stackView.distribution = .equalSpacing
stackView.alignment = .center
stackView.spacing = 8.0
return stackView
}()
...
// Code where I add a lot of other arranged subviews
...
// Code where I also add the image that I want to adjust
horizontalStack.addArrangedSubview(image)
NSLayoutConstraint.activate([
image.centerYAnchor.constraint(equalTo: horizontalStack.centerYAnchor, constant: -3),
])
How do I do this without leading to constraint breaking?
Stack views have a lot of "internal" constraints. It's never (well, never say never) a good idea to try to affect them.
Couple ways to do what you want...
embed your image view in another view, and constrain it with some "bottom padding"
use Alignment Rect Insets for your image
Here's method 2:
Side note: it can be very confusing to name a
UIImageViewobject "image"