Menu
Samaritan's Purse

UIPAGECONTROL

UIPageControl manages control between pages.

EXAMPLE

UIPageControl

INSTRUCTIONS

CREATE

  1. Open Xcode
  2. Click File
  3. Click New
  4. Click Project...
  5. Under iOS, click Application
  6. Click Single View Application
  7. Click Next
  8. After Product Name, type your product name
  9. After Organization Name, type your organization name or leave blank
  10. After Organization Identifier, type your organization identifier
  11. After Language, select Swift
  12. After Devices, select Universal
  13. Uncheck Use Core Data
  14. Click Next
  15. Select a folder
  16. After Source Control, uncheck Create Git repository on
  17. Click Create

EDIT VIEWCONTROLLER.SWIFT

  1. Open ViewController.swift
  2. Copy and paste code

EDIT MAIN.STORYBOARD

  1. Open Main.storyboard
  2. Click View
  3. Click Utilities
  4. Click Show Object Library
  5. Drag Page Control to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Sent Events, after Value Changed, drag + to View Controller
  10. Click valueChanged:
  11. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  12. Click interfaceBuilderPageControl

RUN

  1. Click Product
  2. Click Run

CODE

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var interfaceBuilderPageControl: UIPageControl!
    var defersCurrentPageDisplayPageControl: UIPageControl!
    var blueView: UIView!
    var greenView: UIView!
    var redView: UIView!

    override func prefersStatusBarHidden() -> Bool
    {
        return true
    }
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.lightGrayColor()

        self.redView = UIView()
        self.redView.backgroundColor = UIColor.redColor()
        self.redView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(self.redView)
        self.view.removeConstraints(self.view.constraints())
        self.view.addConstraint(NSLayoutConstraint(item: self.redView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: self.redView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        self.greenView = UIView()
        self.greenView.backgroundColor = UIColor.greenColor()
        self.greenView.hidden = true
        self.greenView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(self.greenView)
        self.view.addConstraint(NSLayoutConstraint(item: self.greenView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: self.greenView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        self.blueView = UIView()
        self.blueView.backgroundColor = UIColor.blueColor()
        self.blueView.hidden = true
        self.blueView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(self.blueView)
        self.view.addConstraint(NSLayoutConstraint(item: self.blueView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: self.blueView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        self.view.bringSubviewToFront(self.interfaceBuilderPageControl)
        self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderPageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        let numberOfPagesPageControl = UIPageControl()
        numberOfPagesPageControl.numberOfPages = 5
        numberOfPagesPageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
        numberOfPagesPageControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(numberOfPagesPageControl)
        self.view.addConstraint(NSLayoutConstraint(item: numberOfPagesPageControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderPageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: numberOfPagesPageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        let currentPagePageControl = UIPageControl()
        currentPagePageControl.numberOfPages = 3
        currentPagePageControl.currentPage = 2
        currentPagePageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
        currentPagePageControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(currentPagePageControl)
        self.view.addConstraint(NSLayoutConstraint(item: currentPagePageControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: numberOfPagesPageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: currentPagePageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        let currentPageIndicatorTintColorPageControl = UIPageControl()
        currentPageIndicatorTintColorPageControl.currentPageIndicatorTintColor = UIColor.blackColor()
        currentPageIndicatorTintColorPageControl.numberOfPages = 3
        currentPageIndicatorTintColorPageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
        currentPageIndicatorTintColorPageControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(currentPageIndicatorTintColorPageControl)
        self.view.addConstraint(NSLayoutConstraint(item: currentPageIndicatorTintColorPageControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: currentPagePageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: currentPageIndicatorTintColorPageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        let pageIndicatorTintColorPageControl = UIPageControl()
        pageIndicatorTintColorPageControl.numberOfPages = 3
        pageIndicatorTintColorPageControl.pageIndicatorTintColor = UIColor.blackColor()
        pageIndicatorTintColorPageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
        pageIndicatorTintColorPageControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(pageIndicatorTintColorPageControl)
        self.view.addConstraint(NSLayoutConstraint(item: pageIndicatorTintColorPageControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: currentPageIndicatorTintColorPageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: pageIndicatorTintColorPageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        let hidesForSinglePagePageControl = UIPageControl()
        hidesForSinglePagePageControl.hidesForSinglePage = true
        hidesForSinglePagePageControl.numberOfPages = 1
        hidesForSinglePagePageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
        hidesForSinglePagePageControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(hidesForSinglePagePageControl)
        self.view.addConstraint(NSLayoutConstraint(item: hidesForSinglePagePageControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: pageIndicatorTintColorPageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: hidesForSinglePagePageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        self.defersCurrentPageDisplayPageControl = UIPageControl()
        self.defersCurrentPageDisplayPageControl.defersCurrentPageDisplay = true
        self.defersCurrentPageDisplayPageControl.numberOfPages = 3
        self.defersCurrentPageDisplayPageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.defersCurrentPageDisplayPageControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
        self.view.addSubview(self.defersCurrentPageDisplayPageControl)
        self.view.addConstraint(NSLayoutConstraint(item: self.defersCurrentPageDisplayPageControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: hidesForSinglePagePageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: self.defersCurrentPageDisplayPageControl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("Update", forState: UIControlState.Normal)
        button.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(button)
        self.view.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.defersCurrentPageDisplayPageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    }
    @IBAction func valueChanged(sender: UIPageControl!)
    {
        switch (sender.currentPage)
        {
            case 0:
                self.redView.hidden = false
                self.greenView.hidden = true
                self.blueView.hidden = true
                break;
            case 1:
                self.redView.hidden = true
                self.greenView.hidden = false
                self.blueView.hidden = true
                break;
            case 2:
                self.redView.hidden = true
                self.greenView.hidden = true
                self.blueView.hidden = false
                break;
            default:
                self.redView.hidden = true
                self.greenView.hidden = true
                self.blueView.hidden = true
                break;
        }
    }
    func touchUpInside(sender: UIButton!)
    {
        self.defersCurrentPageDisplayPageControl.updateCurrentPageDisplay()
    }
}

Apple UIKit