在堆栈视图中显示图像

displaying images in a stack view

是否可以通过编程方式将资产文件夹中的图像显示到 uistackview?如果是这样,你会怎么做?

我已经知道如何创建一个充满标签的堆栈视图。

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

是的。您只需添加一个 ImageView 作为 stackview 的排列子视图。就像标签一样。这是代码 -

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"
  }
}

这是它的呈现方式 -

首先在 imageView 中设置您的图像(在其中设置其约束以更好地控制图像尺寸)和您的 class 控制器下的标签:

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
}()

现在使用分配 fillProportionally 设置您的堆栈视图:

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

在 viewDidLoad 中显示您的堆栈并添加约束:

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
}

我在图片上添加了圆角半径,让它更可爱...