Search code examples
iosswiftxcodeuiimageviewuistackview

Changing images of UIImageView in UIStackView


I have UIStackView (red). When I put in this stackView imageView with picture, this imageView becomes UIView and after that I can't change image even if I want.

I have slider, with image changes counter in my stack view, and button which should change images of my imageView.

my app

my UIImageView

Stack View contains only UIView, not UIIMAgeView. Because of that I can't change imageView image.

StackView contains UIView, Not UIImageView

try to change image


Solution

  • You need to cast the stackView.arrangedSubviews elements to UIImageView

    @objc func buttonAction(_ sender: UIButton) {
        for case let imageView as UIImageView in stackView.arrangedSubviews {
            imageView.image = newImage
        }
    }