1

I am trying to display an image view to the right side of Root stack view. I have set the size and width but for some reason it’s not showing my guess is because the rootstock is in a vertical position. If I put the rootstock view in a horizontal position, the whole thing scatters. How can I fix this? Image below show how it looks like

enter image description here

This how it look like if stack view is in horizontal position. enter image description here

class UserCell: UIView {

  
    var rootStack = UIStackView()
    var userInfoStackView = UIStackView()
    var subtitleStackView = UIStackView()
    var dateInfoStackView = UIStackView()
    var nameLabel = UILabel()
    var compCode = UILabel()
    var captionLabel = UILabel()
    var dateLabel2 = UILabel()
    var imageView = UIImageView()


    override init(frame: CGRect) {

        super.init(frame: frame)

        backgroundColor = .white

        setUPViews()
        addComponents()
        layoutComponents()
    }

    required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    private func setUPViews(){
       
        rootStack.translatesAutoresizingMaskIntoConstraints = false
        rootStack.alignment = .center
        rootStack.axis = .vertical
        rootStack.alignment = .leading
        rootStack.spacing = 3

        subtitleStackView.translatesAutoresizingMaskIntoConstraints = false
        subtitleStackView.spacing = 2

        
        dateInfoStackView.translatesAutoresizingMaskIntoConstraints = false
        dateInfoStackView.spacing = 2

        nameLabel.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        nameLabel.text = "Rolls Royce"
        
        compCode.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
        compCode.text = "(RTYD8NTV001)"

        captionLabel.font = UIFont.systemFont(ofSize: 9, weight: .semibold)
        captionLabel.textColor = .darkGray
        captionLabel.text = "Best Customer Since 07/01/2019"
       
        dateLabel2.font = UIFont.systemFont(ofSize: 13, weight: .bold)
        dateLabel2.text = "July 2022"
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = UIImage(systemName: "print")
        imageView.layer.cornerRadius = 20
        imageView.clipsToBounds = true

        
    }
    private func addComponents() {
         rootStack.addArrangedSubview(imageView)
        
         userInfoStackView.addArrangedSubview(nameLabel)
         userInfoStackView.addArrangedSubview(compCode)
         subtitleStackView.addArrangedSubview(captionLabel)
         
         dateInfoStackView.addArrangedSubview(dateLabel2)
         dateInfoStackView.addArrangedSubview(imageView)
         addSubview(rootStack)
      }
      private func layoutComponents() {
          
          rootStack.addArrangedSubview(userInfoStackView)
          rootStack.addArrangedSubview(subtitleStackView)
          rootStack.addArrangedSubview(dateInfoStackView)
         
          rootStack.snp.makeConstraints { (make) in
              make.edges.equalToSuperview()
          }
          imageView.snp.makeConstraints { (make) in
             make.width.height.equalTo(40)
          }
      }
        
}


let userCell = UserCell(frame: CGRect(x: 0, y: 0, width: 500, height: 60))
PlaygroundPage.current.liveView = userCell

0 Answers0