I am creating a horizontal stack view:
let hs = UIStackView(forAutoLayout: ());
hs.axis = .horizontal
hs.spacing = 0
hs.alignment = .top
hs.distribution = .fill
hs.layoutMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
hs.isLayoutMarginsRelativeArrangement = true
hs.translatesAutoresizingMaskIntoConstraints = false
And then placing a couple children:
let label = UILabel(forAutoLayout: ());
hs.addArrangedSubview(label)
let image = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
image.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 64, leading: 0, bottom: 16, trailing: 16)
hs.addArrangedSubview(image)
The horizontal stack view layout looks right, however I added a layout margin to the image that seems to be ignored. Is there a way to set a margin around specific children in a horizontal stack view?
EDIT:
I get that I can throw the UIImageView inside another view and setup constraints. Or even easier just throw it in another stack view with EdgeInsets:
let imageStack = UIStackView(forAutoLayout: ());
imageStack.axis = .vertical
imageStack.alignment = .fill
imageStack.distribution = .fill
imageStack.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 64, leading: 0, bottom: 16, trailing: 16)
imageStack.isLayoutMarginsRelativeArrangement = true
imageStack.translatesAutoresizingMaskIntoConstraints = false
imageStack.addArrangedSubview(image)
// add imageStack to horizontal stack view, instead of image
hs.addArrangedSubview(imageStack)
Adding to another view or playing with the image rect insets inside the UIImageView seems like overkill just to add margin or padding around a view. Coming from Android where I can just set padding and margin on any view including image views without wrapping in another element this just seems like a crazy amount of work for something so simple.