UIPageControl 和 UIScrollView 不滚动

UIPageControl and UIScrollView not scrolling

我有一个相对简单的 UIViewController,其中 UIScrollView 占据了屏幕的一半,UIImageView 放置在 UIScrollView 中,大小完全相同作为 UIScrollView

UIImageView 之上有一个 UIPageControl。要点是水平滚动并根据数组中的图像数量呈现像滑块一样的图像。问题是滚动视图没有滚动,我不知道为什么。

 @IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var pageControl: UIPageControl!

let imagelist = ["3.jpg", "1.jpg", "2.png", "4.png", "5.png"]

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.delegate = self
    configurePageControl()

    for  i in stride(from: 0, to: imagelist.count, by: 1) {
        var frame = CGRect.zero
        frame.origin.x = self.scrollView.frame.size.width * CGFloat(i)
        frame.origin.y = 0
        frame.size = self.scrollView.frame.size
        scrollView.isPagingEnabled = true
        scrollView.isScrollEnabled = true

        let myImage:UIImage = UIImage(named: imagelist[i])!
        let bgColorFromImage = myImage.averageColor
        imageView.image = myImage
        imageView.contentMode = UIViewContentMode.scaleAspectFit
        imageView.frame = frame


        scrollView.backgroundColor = bgColorFromImage
        scrollView.addSubview(imageView)
    }

    self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width * CGFloat(imagelist.count), height: self.scrollView.frame.size.height)
    pageControl.addTarget(self, action: #selector(changePage), for: UIControlEvents.valueChanged)

}

func configurePageControl() {
    self.pageControl.numberOfPages = imagelist.count
    self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.red
    self.pageControl.pageIndicatorTintColor = UIColor.black
    self.pageControl.currentPageIndicatorTintColor = UIColor.green
    self.imageView.addSubview(pageControl)
}

@objc func changePage() {
    let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
    scrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}

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

这是 UIStoryboard 的样子:

问题是滚动视图本身是用自动布局定位的。一旦你这样做了,你就不能设置 contentSize 来使滚动视图可滚动。您必须使用内部约束来配置内容大小。

在 for 循环中你使用相同的 imageView 对象,你只设置了框架和图像

imageView.frame = frame
imageView.image = myImage

但您必须创建 UIImageView

的新实例

//

假设您在 IB 中有一个 scrollView,它具有 superView 的顶部、前导和尾随约束,并且高度为 200,您可以像这样动态添加 imageViews

class ViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()


        for i in 0..<10 {

            let f = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)

            let imgV = UIImageView(frame: f)

            imgV.image = UIImage(named: "re-fuel.png")

            scrollView.addSubview(imgV)


        }

        scrollView.contentSize = CGSize(width: CGFloat(10) * scrollView.frame.width, height: scrollView.frame.height)
    }
}

结果