Search code examples
iosswiftuitableviewnslayoutconstraint

ios Swift tableView dynamic cells constraints for labels and image programatically


I have labels and an image. I want labels above image. And image without leading and trailing margin constraints.

I have tried the following constraints but can't seem to get it right. And the row height for the cells doesn't adjust accordingly in landscape mode.

    addSubview(videolabel1)
    videolabel1.translatesAutoresizingMaskIntoConstraints = false
    videolabel1.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
    videolabel1.trailingAnchor.constraint(equalTo: trailingAnchor
        , constant: 20).isActive  = true

    videolabel1.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
    videolabel1.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 20).isActive = true


    addSubview(image)
    image.translatesAutoresizingMaskIntoConstraints = false
    image.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    image.trailingAnchor.constraint(equalTo: trailingAnchor).isActive  = true
    image.heightAnchor.constraint(equalToConstant: 150).isActive = true
    image.widthAnchor.constraint(equalTo: videoimage.heightAnchor
        , multiplier: 16/9).isActive = true

    image.topAnchor.constraint(equalTo: videodate.bottomAnchor, constant: 12).isActive = true
    image.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 12).isActive = true

Solution

  • Try using UIStackView's when you want to arrange views in a horizontal or vertical fashion. The UIStackView can take care of most constraints for you. You can try adding a parent UIStackView with vertical orientation, that contains another UIStackView with horizontal orientation (this UIStackView will contain two UILabel's), followed by the UIImageView, and another UILabel.

    parentStackView = UIStackView()
    parentStackView.axis = .vertical
    parentStackView.distribution = .equalSpacing
    parentStackView.translatesAutoresizingMaskIntoConstraints = false
    self.contentView.addSubview(parentStackView)
    parentStackView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
    parentStackView.widthAnchor.constraint(equalTo: self.contentView.widthAnchor, multiplier: 1).isActive = true
    parentStackView.heightAnchor.constraint(equalTo: self.contentView.heightAnchor, multiplier: 1).isActive = true
    
    parentStackView.isLayoutMarginsRelativeArrangement = true
    parentStackView.addArrangedSubview(stackView)
    stackView.axis = .horizontal
    stackView.distribution = .equalSpacing
    stackView.spacing = 10
    stackView.addArrangedSubview(label)
    label.text = "First Label";
    stackView.addArrangedSubview(label2)
    label2.text = "Second Label";
    parentStackView.addArrangedSubview(imageView);
    imageView.image = UIImage(named: "imageview");
    NSLayoutConstraint.activate([imageView.heightAnchor.constraint(equalToConstant:UIScreen.main.bounds.width / 2) ]);
    imageView.contentMode = .scaleAspectFill;
    imageView.clipsToBounds = true;
    parentStackView.addArrangedSubview(label3)
    label3.text = "Vertical Label below Image";
    

    The result will look something like:

    enter image description here