UIProgressView
Description
UIProgressView displays the progress of a task over time.
Example
Instructions
Create
- Open Xcode
- Click File
- Click New
- Click Project...
- Under iOS, click Application
- Click Single View Application
- Click Next
- After Product Name, type your product name
- After Organization Name, type your organization name or leave blank
- After Organization Identifier, type your organization identifier
- After Language, select Swift
- After Devices, select Universal
- Uncheck Use Core Data
- Click Next
- Select a folder
- After Source Control, uncheck Create Git repository on
- Click Create
Edit ViewController.swift
- Open ViewController.swift
- Copy and paste code
Edit Main.storyboard
- Open Main.storyboard
- Click View
- Click Utilities
- Click Show Object Library
- Drag Progress View to View
- Click View
- Click Utilities
- Click Show Connections Inspector
- Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
- Click interfaceBuilderProgressView
Add Image
- Click File
- Click Add Files to "UIProgressView" ...
- Select image
- After Destination, check Copy items if needed
- After Added folders, select Create groups
- After Add to targets, check UIProgressView
- Click Add
Run
- Click Product
- Click Run
Code
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var interfaceBuilderProgressView: UIProgressView!
override func prefersStatusBarHidden() -> Bool
{
return true
}
override func viewDidLoad()
{
super.viewDidLoad()
self.view.removeConstraints(self.view.constraints())
self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderProgressView, 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("Animate", forState: UIControlState.Normal)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(button)
self.view.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderProgressView, 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))
let progressProgressView = UIProgressView()
progressProgressView.progress = 0.5
progressProgressView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(progressProgressView)
self.view.addConstraint(NSLayoutConstraint(item: progressProgressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: button, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: progressProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: progressProgressView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
let progressViewStyleProgressView = UIProgressView()
progressViewStyleProgressView.progress = 0.5
progressViewStyleProgressView.progressViewStyle = UIProgressViewStyle.Bar
progressViewStyleProgressView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(progressViewStyleProgressView)
self.view.addConstraint(NSLayoutConstraint(item: progressViewStyleProgressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: progressProgressView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: progressViewStyleProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: progressViewStyleProgressView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
let progressTintColorProgressView = UIProgressView()
progressTintColorProgressView.progress = 0.5
progressTintColorProgressView.progressTintColor = UIColor.redColor()
progressTintColorProgressView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(progressTintColorProgressView)
self.view.addConstraint(NSLayoutConstraint(item: progressTintColorProgressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: progressViewStyleProgressView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: progressTintColorProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: progressTintColorProgressView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
let trackTintColorProgressView = UIProgressView()
trackTintColorProgressView.progress = 0.5
trackTintColorProgressView.trackTintColor = UIColor.redColor()
trackTintColorProgressView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(trackTintColorProgressView)
self.view.addConstraint(NSLayoutConstraint(item: trackTintColorProgressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: progressTintColorProgressView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: trackTintColorProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: trackTintColorProgressView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
let progressImageProgressView = UIProgressView()
progressImageProgressView.progress = 0.5
progressImageProgressView.progressImage = UIImage(named: "HappyFace") // not working properly as of iOS8
progressImageProgressView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(progressImageProgressView)
self.view.addConstraint(NSLayoutConstraint(item: progressImageProgressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: trackTintColorProgressView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: progressImageProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: progressImageProgressView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
let trackImageProgressView = UIProgressView()
trackImageProgressView.progress = 0.5
trackImageProgressView.setTranslatesAutoresizingMaskIntoConstraints(false)
trackImageProgressView.trackImage = UIImage(named: "HappyFace") // not working properly as of iOS8
self.view.addSubview(trackImageProgressView)
self.view.addConstraint(NSLayoutConstraint(item: trackImageProgressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: progressImageProgressView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: trackImageProgressView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 0, constant: 30))
self.view.addConstraint(NSLayoutConstraint(item: trackImageProgressView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
}
func touchUpInside(sender: UIButton!)
{
self.interfaceBuilderProgressView.setProgress(1, animated: true)
}
}