Horizo​​ntal ScrollView 不适合它的子 ImageViews?

Horizontal ScrollView not fit its sub ImageViews?

我想要带图像视图的水平滚动视图,但图像不适合滚动。我正在执行这段代码,但没有得到正确的结果 这是我的代码

@IBOutlet weak var upperScroll: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isHidden = true
        // Do any additional setup after loading the view.
        var logoImage: [UIImage] = [
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
        ]

        upperScroll.isScrollEnabled = true
        let scrollWidth: Int = Int(self.view.frame.width)
        upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
        upperScroll.frame = CGRect(x: 0, y: 51, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height)
        upperScroll.backgroundColor = UIColor.red
        var xOffset: Int = 0
        for index in 0..<logoImage.count {
            let img = UIImageView(frame: CGRect(x: CGFloat(xOffset), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
            img.image = logoImage[index]
            upperScroll.addSubview(img)
            xOffset += 100
        }
        view.addSubview(upperScroll)
        upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth + xOffset)), height: 110)

    }

but i want this

谁能帮帮我?谢谢

根据您的要求,xOffset += 100应该改为xOffset += scrollWidth。内容大小设置为:

upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth * logoImage.count)), height: 110)

并记得为滚动视图启用分页。

只需删除您的 appdelegate 代码并添加我的功能 还设置滚动视图约束,如前导、尾随、顶部、底部零

  func scrollImage() {
            super.viewDidLoad()
            self.navigationController?.navigationBar.isHidden = true
            // Do any additional setup after loading the view.
            var logoImage: [UIImage] = [
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!]

            upperScroll.isPagingEnabled = true

            upperScroll.isScrollEnabled = true

            let scrollWidth: Int = Int(self.view.frame.width)
,
            upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
            print(upperScroll.frame.size.width)
            upperScroll.backgroundColor = UIColor.red
            for index in 0..<logoImage.count {
                let xPosition = self.view.frame.width * CGFloat(index)
                upperScroll.layoutIfNeeded()
                let img = UIImageView(frame: CGRect(x: CGFloat(xPosition), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
                img.layoutIfNeeded()
                img.image = logoImage[index]
                upperScroll.addSubview(img)
            }
            view.addSubview(upperScroll)
            upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth) * logoImage.count), height: self.upperScroll.frame.height)

        }

我希望它能节省您的时间并且工作得很好

首先,你需要两个scroll view和PageControl的outlet,

 var contentWidth:CGFloat = 0.0

// MARK: - Welcome screen with scroll view use page controll

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    scrollView.delegate = self
    let myImages = ["intro1_1.png", "intro2_2.png", "intro3_3.png"] // images which loaded in assets
    let imageWidth:CGFloat = view.frame.width
    let imageHeight:CGFloat = view.frame.height
    var xPosition:CGFloat = 0 //setup your position
    var scrollViewSize:CGFloat=0 //setup your position

    for image in myImages {
        let myImage:UIImage = UIImage(named: image)!
        let myImageView:UIImageView = UIImageView()
        myImageView.image = myImage
        myImageView.frame.size.width = imageWidth
        myImageView.frame.size.height = imageHeight
        myImageView.frame.origin.x = xPosition
        myImageView.frame.origin.y = 0
        scrollView.addSubview(myImageView)
        xPosition += imageWidth
        scrollViewSize += imageWidth
    }
    scrollView.contentSize = CGSize(width: scrollViewSize, height: imageHeight)
    scrollView.contentSize = CGSize(width: view.frame.width * 3, height: view.frame.height) //here you setup how many pages in scroll will

}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth:CGFloat = scrollView.frame.width
    let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth / 2)/pageWidth)+1
    self.pageControll.currentPage = Int(currentPage)
}

希望能帮到你