6onn0g
Last Updated: February 25, 2016
·
3.652K
· girobai
 avatar 003

Adding progress icon programmatically to a new UIView

Useful snippet to add a progress icon in a new UIView

Picture

Picture

add the loadView() function on VIewController (using Single View App template)

override func loadView() {
    super.loadView()

    var baseView = UIView()
    baseView.backgroundColor = UIColor(red: 13/255, green: 44/255, blue: 75/255, alpha: 1)
    self.view = baseView

    var progressIcon = UIActivityIndicatorView()
    progressIcon.setTranslatesAutoresizingMaskIntoConstraints(false)
    progressIcon.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    view.addSubview(progressIcon)
    progressIcon.startAnimating()

    var constraints = [NSLayoutConstraint]()
    constraints.append(NSLayoutConstraint(
        item: progressIcon,
        attribute: .CenterX,
        relatedBy: .Equal,
        toItem: view,
        attribute: .CenterX,
        multiplier: 1,
        constant: 0)
    )
    constraints.append(NSLayoutConstraint(
        item: progressIcon,
        attribute: .CenterY,
        relatedBy: .Equal,
        toItem: view,
        attribute: .CenterY,
        multiplier: 1,
        constant: 0)
    )

    view.addConstraints(constraints)

}
Say Thanks
Respond