Change page on UIScrollView

后端 未结 12 1306
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 18:45

I have a UIScrollView with 10 pages. I am able to flick between them. I also want to have 2 buttons (a back button and a next button) which when touched will go to the previ

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 19:37

    First create a UIScrollView extension like:

    extension UIScrollView {
    
        func setCurrentPage(position: Int) {
            var frame = self.frame;
            frame.origin.x = frame.size.width * CGFloat(position)
            frame.origin.y = 0
            scrollRectToVisible(frame, animated: true)
        }
    
    }
    

    then just call:

    self.scrollView.setCurrentPage(position: 2)  // where 2 is your desired page
    

提交回复
热议问题