通过scrollView分页显示不正确

时间:2018-08-21 13:43:24

标签: ios xcode pagination uiscrollview

我不明白为什么iPhone 4,7可以正确显示,但在iPhone 5,5上却不能正确显示。我想通过ScrollView进行分页。

我该怎么做才能在所有设备上正确显示?

func showScrollView() {
        var photoSlides = [Slides]()
        for image in photo {
            let slide: Slides = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slides
            slide.slideImage.image = image.image
            photoSlides.append(slide)
        }
        setupSlideScrollView(slides: photoSlides)
    }

    func setupSlideScrollView(slides : [Slides]) {
        scrollView.contentSize = CGSize(width: scrollView.frame.width * CGFloat(slides.count), height: 200)
        scrollView.isPagingEnabled = true

        for i in 0 ..< slides.count {
            slides[i].frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(slides[i])
        }
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageIndex = round(scrollView.contentOffset.x/scrollView.frame.width)
        pageControl.currentPage = Int(pageIndex)
    }

https://yadi.sk/i/ipS5WchP3aRtZ9-正确(iPhone 8)

https://yadi.sk/i/SPmriRFn3aRtgL-不正确(iPhone 8 Plus)

1 个答案:

答案 0 :(得分:0)

如果您使用情节提要或xib设计用户界面,然后对于屏幕的某些部分想要框架大小来计算其他控件的框架,则必须在该控件上使用layoutIfNeeded方法。也就是说,就您而言,您可以执行以下操作

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated) 

    scrollView.layoutIfNeeded()

    // THEN CALL YOUR FUNCTION WHICH SETUP YOUR OTHER VIEWS
    showScrollView()
}