如果 translatesAutoresizingMaskIntoConstraints = false,滚动视图不工作

scrollview not working if translatesAutoresizingMaskIntoConstraints = false

我在使用 translatesAutoresizingMaskIntoConstraints 时遇到一些问题。 我正在创建一个滚动视图,您可以在其中向左或向右滑动。每次你得到另一个图像。但出于某种原因,如果 translatesAutoresizingMaskIntoConstraints = false,我无法滑动。

这是 translatesAutoresizingMaskIntoConstraints = false(在这种情况下,我无法向左或向右滑动)

但是如果我设置 translatesAutoresizingMaskIntoConstraints = true 它看起来像这样,我可以向左和向右滑动。问题是布局乱了。

我的代码:

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

创建时添加滚动视图框架

var scrollView: UIScrollView = {
    let scrollView = UIScrollView(frame:.zero)
    //other properties
    return scrollView
}()

ImageView 约束与 ScrollView 相同。 这就是为什么你不能滑动的原因,因为你所有的图像都有相同的约束,并且它们一个接一个地放置。 你应该这样做:

imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.frame.size = contentView.frame.size
imageView.frame.origin = CGPoint(x: imageIndex * contentView.frame.size.width, y: 0.0)

你可能需要添加约束,你可以试试

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