title: Display current Page number after/before
body: <p>I was faced with the problem displaying the <strong>current page</strong>. I use <strong>scrollView</strong> with <strong>pageControl</strong> and i have a <strong>label</strong> on which i display <strong>current page</strong> and <strong>total pages.</strong> If i scroll on next image my label are change <strong>+ 1 page,</strong> but if i scroll on previous page my label don't change on <strong>- 1</strong>, my label are change only if i scroll twice on previous page.</p>
  
  <p>My code is here:</p>
  
  <pre><code>class ScrollImageViewController: UIViewController, UIScrollViewDelegate {
  
      @IBOutlet weak var pageControl: UIPageControl!
      @IBOutlet weak var scrollImage: UIScrollView!
      @IBOutlet weak var numberOfPagesLabel: UILabel!
  
      private var imageView: UIImageView!
  
      var imagesArray = [String]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          self.pageControl.numberOfPages = imagesArray.count
  
          self.numberOfPagesLabel.layer.cornerRadius = 15.0
          self.numberOfPagesLabel.clipsToBounds = true
          self.numberOfPagesLabel.text = "\(self.pageControl.currentPage)/\(self.imagesArray.count)"
  
          for i in 0..&lt;imagesArray.count {
  
              imageView = UIImageView()
              imageView.sd_setImage(with: URL(string: imagesArray[i]))
              imageView.contentMode = .scaleAspectFit
              let xPosition = self.view.frame.width * CGFloat(i)
              imageView.frame = CGRect(x: xPosition, y: 0, width: self.scrollImage.frame.width, height: self.scrollImage.frame.height)
  
              scrollImage.contentSize.width = scrollImage.frame.width * CGFloat(i + 1)
              scrollImage.addSubview(imageView)
  
          }
      }
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
  
          let currentPage = scrollImage.contentOffset.x / UIScreen.main.bounds.size.width
  
          if pageControl.currentPage &lt; imagesArray.count {
              self.pageControl.currentPage += 1
              self.numberOfPagesLabel.text = "\(self.pageControl.currentPage + 1)/\(self.imagesArray.count)"
          } else if pageControl.currentPage != 0 {
              self.pageControl.currentPage -= 1
              self.numberOfPagesLabel.text = "\(self.pageControl.currentPage - 1)/\(self.imagesArray.count)"
          }    
          pageControl.currentPage = Int(currentPage)
      }
  }
  </code></pre>
  
  <p>And .gif for a better understanding, look at the label above the image</p>
  
  <p><a href="https://i.stack.imgur.com/DlUEY.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DlUEY.gif" alt=".gi"></a></p>