Search code examples
iosswiftuiimageviewuiimagescrollview

scrollview not working if translatesAutoresizingMaskIntoConstraints = false


I have some issues using translatesAutoresizingMaskIntoConstraints. I am creating a scrollview where you can swipe to the left or right. Every time you get a another image. But for some reason I can't swipe if translatesAutoresizingMaskIntoConstraints = false.

This is with translatesAutoresizingMaskIntoConstraints = false (in this case, I can't swipe left or right)

enter image description here

But if I set translatesAutoresizingMaskIntoConstraints = true it look like this and I can swipe to the left and right. The problem is, that the layout is messed up.

enter image description here

My code:

var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
        scrollView.isPagingEnabled = true
        return scrollView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
scrollView.frame = contentView.frame
        contentView.addSubview(scrollView)

        scrollView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
        scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
        scrollView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
}

var frame = CGRect.zero
    func viewTutorial() {
        for i in 0..<arrayOfTutorilImages.count {
        frame.origin.x = scrollView.frame.size.width  * CGFloat((i))
            frame.size = scrollView.frame.size

            let imageView = UIImageView(frame: frame)
            imageView.image = UIImage(named: arrayOfTutorilImages[i])
            imageView.contentMode = .scaleAspectFit
            imageView.backgroundColor = .red
            imageView.translatesAutoresizingMaskIntoConstraints = false
            self.scrollView.addSubview(imageView)


            imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
            imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
            imageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
            imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true

        }

        scrollView.contentSize = CGSize(width: (scrollView.frame.size.width * CGFloat(arrayOfTutorilImages.count)), height: scrollView.frame.size.height)
        scrollView.delegate = self
    }

extension TutorialViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
        pageControl.currentPage = Int(pageNumber)
    }
}

Solution

  • You need to add constraints probably , You can try

    class TutorialViewController: UIViewController {
    
        var scrollView: UIScrollView = {
                let scrollView = UIScrollView()
                scrollView.showsHorizontalScrollIndicator = false
                scrollView.showsVerticalScrollIndicator = false
                scrollView.translatesAutoresizingMaskIntoConstraints = false
                scrollView.alwaysBounceVertical = false
                scrollView.alwaysBounceHorizontal = false
                scrollView.isPagingEnabled = true
                return scrollView
            }()
    
            override func viewDidLoad() {
                super.viewDidLoad() 
                contentView.addSubview(scrollView)
    
                scrollView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
                scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
                scrollView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
                scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
    
                viewTutorial()
        }
    
    
            func viewTutorial() {
                var con:UIView = scrollView
                for i in 0..<arrayOfTutorilImages.count {
    
                    let imageView = UIImageView(frame: CGRect.zero)
                    imageView.image = UIImage(named: arrayOfTutorilImages[i])
                    imageView.contentMode = .scaleAspectFit
                    imageView.backgroundColor = .red
                    imageView.translatesAutoresizingMaskIntoConstraints = false
                    self.scrollView.addSubview(imageView)
    
                    if con == scrollView {
                         imageView.leftAnchor.constraint(equalTo: con.leftAnchor, constant: 0).isActive = true
                    }
                    else {
                         imageView.leftAnchor.constraint(equalTo: con.rightAnchor, constant: 0).isActive = true
                    }
    
                    imageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
    
                    if i == arrayOfTutorilImages.count - 1 {
                        imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 0).isActive = true
    
                    }
                    imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
                  imageView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
                    con = imageView
    
    
                }
    
                scrollView.delegate = self
            }
    }
    
    extension TutorialViewController: UIScrollViewDelegate {
         func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
             let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
    
         }
     }
    

    Don't set frames when using constrraints