11.1 C
London
Thursday, February 29, 2024

ios – UIStack view doesn’t organize merchandise accordingly


I’ve create stack view programmatically. I’ve given constrains for it. I’ve added the sector into stack view with addArrangedSubview perform. I’m anticipating the stack view ought to organize the cell property (one under the opposite one) see the anticipated screenshot . but it surely not arranging the view accurately. Additionally there are spacing challenge between the picture view as properly.

Right here is the code for cell ..

class SimilierMovieCell: UICollectionViewCell {
    
    static let identifier = "CompanyCell"
    
    non-public lazy var similarMovieImage: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleToFill
        return imageView
    }()
    
    lazy var movieTitle: UILabel = {
        let movieTitle = UILabel()
        movieTitle.translatesAutoresizingMaskIntoConstraints = false
        movieTitle.font = UIFont.systemFont(ofSize: 20)
        return movieTitle
    }()
  
    //Stack View
    let stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.translatesAutoresizingMaskIntoConstraints = false;
        stackView.distribution = UIStackView.Distribution.equalSpacing
        stackView.alignment = UIStackView.Alignment.heart
        stackView.spacing = 50.0
        return stackView
    }()

    func configureCell(film: Film) {
        
        contentView.addSubview(stackView)
        stackView.addArrangedSubview(similarMovieImage)
        stackView.addArrangedSubview(movieTitle)

        //Constraints
        let safeArea = contentView.safeAreaLayoutGuide

        NSLayoutConstraint.activate([
            stackView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
            stackView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor),
            
            similarMovieImage.topAnchor.constraint(equalTo: stackView.topAnchor),
            similarMovieImage.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor),
            similarMovieImage.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
            similarMovieImage.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor),
            
            movieTitle.topAnchor.constraint(equalTo: similarMovieImage.bottomAnchor),
            movieTitle.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor),
            movieTitle.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
            movieTitle.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor),
          
        ])
        
       
        if let path = film.posterPath {
            similarMovieImage.dm_setImage(posterPath: path)
        } else {
            similarMovieImage.picture = nil
        }
        
        movieTitle.textual content = film.title
    }
}

Right here is the anticipated consequence.

expected result

Right here is the consequence I obtained ..

result currently

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here