滚动视图不显示图像而是滚动?

时间:2019-01-11 22:18:00

标签: ios swift uikit scrollview

我正在尝试学习IOS开发的基础知识,并且我一直在尝试不同的观点。 我试图制作一个滚动视图,以显示可正确滚动的图像阵列,但没有图像出现?

import UIKit

class ViewController: UIViewController {
    var imagesArray = [UIImage]()

    let scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.isPagingEnabled = true
        scroll.showsVerticalScrollIndicator = false
        scroll.showsHorizontalScrollIndicator = false
        scroll.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        return scroll
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setScrollView()
    }

    func setScrollView(){
        self.view.addSubview(scrollView)
        imagesArray = [UIImage(named:"gamerrr"),UIImage(named:"family"),UIImage(named:"gamerrr"),UIImage(named:"family"),UIImage(named:"family")] as! [UIImage]

        setUpImages(imagesArray)
    }

    func setUpImages(_ images:[UIImage]){
        for i in 0..<images.count {
            // container of each image
            let imageView = UIImageView()
            // shifting to next image
            let xPos = UIScreen.main.bounds.width * CGFloat(i)
            // width and height of the screen
            imageView.frame = CGRect(x: xPos, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            imageView.contentMode = .scaleAspectFit
             // shifting to next image
            scrollView.contentSize.width = scrollView.frame.width * CGFloat(i+1)
            scrollView.addSubview(imageView)
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可能需要设置图像

let imageView = UIImageView(image:images[i])