Search code examples
iosswiftuiscrollviewuipagecontrol

Scroll view with page control to display images in iOS


I am trying to implement a scroll view with page control to display images in iOS using Swift, to get an output like this:

Required output

The scroll view here is a part of another view controller. I googled but got no help to get the required output. Can anyone please help me?


Solution

  • The answer is a bit late. So, may be helpful for someone else:

    Swift 3x:

    First of all, give the delegation like this:

    class YOUR_VIEW_CONTROLLER: UIViewController, UIScrollViewDelegate 
    

    Now connect the outlets of UIScrollView and UIPageControl like this:

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    

    NOTE:I'm using colours array. You can use image array instead.

    var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
    var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)
    

    Now just copy and paste the below code in your class:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        configurePageControl()
    
        self.view.bringSubview(toFront: pageControl)
        scrollView.delegate = self
        self.view.addSubview(scrollView)
        for index in 0..<4 {
    
            frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
            frame.size = self.scrollView.frame.size
    
            let subView = UIView(frame: frame)
            subView.backgroundColor = colors[index]
            self.scrollView.addSubview(subView)
        }
    
        self.scrollView.isPagingEnabled = true
    
    
        self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4,height: self.scrollView.frame.size.height)
    
        pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
    
    }
    
    func configurePageControl() {
        // The total number of pages that are available is based on how many available colors we have.
        self.pageControl.layer.zPosition = 1
    
        self.pageControl.numberOfPages = colors.count
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.red
        self.pageControl.pageIndicatorTintColor = UIColor.black
        self.pageControl.currentPageIndicatorTintColor = UIColor.green
        self.view.addSubview(pageControl)
    
    }
    
    // MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
    func changePage(sender: AnyObject) -> () {
        let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
        scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    
        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = Int(pageNumber)
    }