简体   繁体   English

计算UIScrollView的内容大小

[英]Calculating content size for UIScrollView

 func setupScrollView(){
        let scrollWidth = scrollView.frame.size.width
        let scrollHeight = scrollView.frame.size.height

        scrollView.isPagingEnabled = true

        var contentWidth : CGFloat = 0.0

        for x in 0...bannerImages.count - 1 {

            let image = (UIImage(named: bannerImages[x]))
            let imageView = UIImageView(image: image)
            imageView.clipsToBounds = true

            var newX : CGFloat = 0.0
            newX = scrollWidth / 2 + scrollWidth * CGFloat(x)
            contentWidth += newX
            scrollView.addSubview(imageView)
            imageView.frame = CGRect(x: newX - itemWidth / 2, y: scrollView.frame.midY - itemWidth / 2, width: itemWidth, height: itemWidth)

        }

        scrollView.contentSize = CGSize(width:contentWidth, height: scrollHeight)

    }

I've got this in my viewDidAppear and I have no autolayout in play. 我已经在viewDidAppear中找到了这个,并且没有自动布局。

My content size width is adding a ton of space that shouldn't be there. 我的内容大小宽度增加了不该存在的大量空间。 I've got 3 images I'm showing and I can scroll past the 3rd image with paging 2 more times. 我已经显示了3张图像,并且可以滚动2次来翻阅第三张图像。

Any ideas? 有任何想法吗?

I am not sure what you are trying to accomplish but I think your math is off. 我不确定您要完成什么,但我认为您的数学过高了。 If you are trying to stack three images on the x axes in a scrollview and set the scrollview content size to the sum of your image widths then calculate your newX like this newX = itemWidth * CGFloat(x) also set image frame like this imageView.frame = CGRect(x: newX, y: scrollView.frame.midY - itemWidth / 2, width: itemWidth, height: itemWidth) . 如果您试图在滚动视图中的x轴上堆叠三个图像,并将滚动视图的内容大小设置为图像宽度的总和,则像这样计算newX = itemWidth * CGFloat(x)还要设置图像框架,例如newX = itemWidth * CGFloat(x) imageView.frame = CGRect(x: newX, y: scrollView.frame.midY - itemWidth / 2, width: itemWidth, height: itemWidth)

I don't think this is a right way of implementing ScrollView ! 我认为这不是实现ScrollView的正确方法! Probably not a best practice. 可能不是最佳做法。 I think you should have a child view in your ScrollView call it contentView and then set appropriate AutoLayout Constraints for that view. 我认为您应该在ScrollView有一个child view ,将其称为contentView ,然后为该视图设置适当的AutoLayout Constraints After that add each image to contentView . 之后,将每个图像添加到contentView That way things would be easier. 这样一来,事情就会变得简单。

Check out this blog post https://www.natashatherobot.com/ios-autolayout-scrollview/ 查看此博客文章https://www.natashatherobot.com/ios-autolayout-scrollview/

I Update this code check comments.... 我更新此代码检查注释。

    func setupScrollView(){
        let scrollWidth = scrollView.frame.size.width
        let scrollHeight = scrollView.frame.size.height

        scrollView.isPagingEnabled = true

        var contentWidth : CGFloat = 0.0

        for x in 0...bannerImages.count - 1 {

            let imageView = UIImageView(image: bannerImages[x])
            imageView.clipsToBounds = true

            var newX : CGFloat = 0.0
            newX = scrollWidth / 2 + scrollWidth * CGFloat(x)
//            contentWidth += newX
            // This is wrong here. Because you already add scrollview widht in newx and by this line you are adding it again.
            contentWidth = newX
            scrollView.addSubview(imageView)
            imageView.frame = CGRect(x: newX - itemWidth / 2, y: scrollView.frame.midY - itemWidth / 2, width: itemWidth, height: itemWidth)

        }
        contentWidth = contentWidth + (itemWidth / 2)
        // Add this line also beacuse newx store half width so you have to add extra half space in content width
        scrollView.contentSize = CGSize(width:contentWidth, height: scrollHeight)

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM