Well, not sure if I completely understand your question, but let's see ...
I'm playing around with the idea of a card game and the generic views in the horizontal stack are supposed to be the stacks of cards. So, for each card on the stack, I add an image view to the generic view.
So, the hierarchy is actually Root View -> stacked view -> view [0-n] -> image view [0-n]
The images I'm trying to use are considerably larger than what the stacked view allocates for each card stack's width. I expect everything to work if the width of the image matched exactly what the stacked view determines to be the right width. However that can change depending on number of card stacks or even display size. Also, if the image was smaller than the width the stacked view allocates for the subviews, I expect things would work as expected, because the images would be scaled up to fill the space. But it doesn't seem right to select the smallest possible images for the smallest display out there and let scaling accommodate larger displays.
In the above view hierarchy, everything to the left of the image view is designed in the UI builder.
I think it's when I programatically add the image views to their respective superviews, their frame and bounds aren't set automatically.
When I create the image view, the bounds are apparently set based on the image passed in the constructor, and they stay the same after being added to the hierarchy below the stacked view. It seems to work, if I set the bounds programatically based on what the stacked view calculated for its direct subviews, like this:
func updateStacks() {
for stack in 0...7 {
for stackCard:Int in 0...dealt[stack].count-1 {
let image = UIImage(named: getImageNameForCard(cardType: dealt[stack][stackCard]))
let imageView = UIImageView(image: image)
imageView.contentMode = UIView.ContentMode.scaleAspectFit
//imageView.translatesAutoresizingMaskIntoConstraints = false;
imageView.isOpaque = true;
imageView.preservesSuperviewLayoutMargins = true;
var iVbounds:CGRect = CGRect(x: 0, y: 0, width: stackTopViews[stack].bounds.width, height: stackTopViews[stack].bounds.height)
imageView.bounds = iVbounds
stackTopViews[stack].addSubview(imageView)
imageView.frame.origin.x = 0
if (stackCard > 0) {
imageView.frame.origin.y = 0 + CGFloat((30 * stackCard));
} else {
imageView.frame.origin.y = 0
}
}
}
}
I guess I was expecting for the image views to automatically inherit their bounds from their superior view, when they're being added. Maybe I was expecting too much.