1

Is it possible to display an image from the assets folder to a uistackview programmatically? If so, how would you go about doing it?

I already know how to create a stack view filled with labels.

    fileprivate lazy var stack: UIStackView = {
        let stack = UIStackView(arrangedSubviews: [goalCompleteLabel, completeMoreGoalsLabel])
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.axis = .vertical
        return stack
    } ()

2 Answers2

1

First set your image in imageView (set in it its constraints for more control of image dimension) and your label under your class controller:

let image: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "yourImage")?.withRenderingMode(.alwaysOriginal)
    imageView.backgroundColor = .gray
    imageView.layer.cornerRadius = 8
    imageView.clipsToBounds = true
    imageView.contentMode = .scaleAspectFill
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 200).isActive = true
    return imageView
}()

let completeMoreGoalsLabel: UILabel = {
    let label = UILabel()
    label.text = "Dummytext"
    label.textAlignment = .center
    return label
}()

now set your stack view with distribution fillProportionally:

lazy var stack: UIStackView = {
    let stackView = UIStackView(arrangedSubviews: [image, completeMoreGoalsLabel])
    stackView.axis = .vertical
    stackView.distribution = .fillProportionally
    stackView.translatesAutoresizingMaskIntoConstraints = false
    return stackView
}()

in viewDidLoad present your stack and add constraints:

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .darkGray

    view.addSubview(stack)
    stack.widthAnchor.constraint(equalToConstant: 200).isActive = true
    stack.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    stack.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    stack.heightAnchor.constraint(equalToConstant: 250).isActive = true //200 imageHeight + 50 label height
}

I add corner radius on image to make it more cute...

enter image description here

Fabio
  • 5,432
  • 4
  • 22
  • 24
0

Yes. You simply need to add an ImageView as an arranged subview of your stackview. Just like the labels. Here's the code -

class StackViewController: UIViewController {

  var stackView   = UIStackView()
  var label       = UILabel()
  var imageView   = UIImageView()

  override func viewDidLoad() {
    super.viewDidLoad()
    configStackView()
  }

  func configStackView() -> Void {

    // Add StackView as SubView
    view.addSubview(stackView)

    // Set StackView properties
    stackView.axis = .vertical
    stackView.alignment = .center
    stackView.distribution = .equalSpacing

    // Set imageView as 1st arranged subview of stackview
    stackView.addArrangedSubview(imageView)
    configImageView()

    // Set Label as 2nd arranged subview of stackview
    stackView.addArrangedSubview(label)
    configLabel()

    // Set StackView Constraints
    setStackViewCostraints()
  }

  func setStackViewCostraints() -> Void {
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
    stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
    stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
  }


  func configImageView() -> Void {
    imageView.image = UIImage(named: "bolt")
    imageView.contentMode = .scaleAspectFit

    // Set Constraints (ideally in a separate function)
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
  }

  func configLabel() -> Void {
    label.text = "Label"
  }
}

Here's the how it renders -

enter image description here

Animesh K
  • 78
  • 6