Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

UIScrollView page is off-center after scrolling

I am using Swift 4, iOS 12. I have a ScrollView with pagination. when I scroll back and forth, the views end up getting offset and are no longer centered in the middle of the screen. This isn't too noticeable on smaller screens, but on an iPad it is clearly noticeable. this is my viewDidLoad

    swiperScrollView.isPagingEnabled = true
    swipersPageControl.numberOfPages = swiperPageData.count
    swipersPageControl.currentPage = 0
    view.bringSubview(toFront: swipersPageControl)

    swiperScrollView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(swiperPageData.count), height: swiperScrollView.frame.height)
    for (i, data) in swiperPageData.enumerated() {
        if let swiperView = ... {
            swiperView.frame.origin.x = CGFloat(i) * self.view.bounds.size.width
            swiperScrollView.addSubview(swiperView)
            let widthConstraint = NSLayoutConstraint.init(item: swiperView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1, constant: 0)
            let centerConstraint = NSLayoutConstraint.init(item: swiperView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
            NSLayoutConstraint.activate([widthConstraint, centerConstraint])
        }
    }

Any idea why this is happening?

Comments