su1t1a
Last Updated: August 09, 2018
·
82.55K
· itseranga
892414 10151468467596730 1847316210 o

IOS customized activity indicator with Swift

Default activity indicator

  • Following function will add default activity indicator to a view.
func showActivityIndicatory(uiView: UIView) {
    var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    actInd.center = uiView.center
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle =
                    UIActivityIndicatorViewStyle.WhiteLarge
    uiView.addSubview(actInd)
    activityIndicator.startAnimating()
}
  • Output will be looks like below

Picture

Customized activity indicator

  • Following function add customized activity indicator to a view
func showActivityIndicatory(uiView: UIView) {
    var container: UIView = UIView()
    container.frame = uiView.frame
    container.center = uiView.center
    container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3)

    var loadingView: UIView = UIView()
    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    actInd.activityIndicatorViewStyle =
                UIActivityIndicatorViewStyle.WhiteLarge
    actInd.center = CGPointMake(loadingView.frame.size.width / 2,
                loadingView.frame.size.height / 2);
    loadingView.addSubview(actInd)
    container.addSubview(loadingView)
    uiView.addSubview(container)
    actInd.startAnimating()
}
  • In this function I have added partially transparent overlay to view and display activity indicator in rounded rectangle

  • Following is the output

Picture

  • Complete source code

https://github.com/erangaeb/dev-notes/blob/master/swift/ViewControllerUtils.swift

Say Thanks
Respond

11 Responses
Add your response

16862
Me

Modified source code with a singleton - https://github.com/Isuru-Nanayakkara/Swift-ProgressView

over 1 year ago ·
20714
None

nice...can you also tell me if there is a simple way to add custom image as the activity indicator?

over 1 year ago ·
20764
None

Hi, I was add this code to my app and I have an issue. It doesn't show in the centre. It show bit blow to the centre. Any ideas please?

/Oshadha

over 1 year ago ·
29800

Drag CustomActivityIndicatorView.swift to your project and add some image which should be animated in custom activity indicator view. It could be simple declared this way in your class:

lazy private var activityIndicator : CustomActivityIndicatorView = {
let image : UIImage = UIImage(named: "loading")!
return CustomActivityIndicatorView(image: image)
}()
where "loading" is your image in png format.

Then simple add it to your view:

self.view.addSubview(activityIndicator)
And run animation:

activityIndicator.startAnimating()
To stop animation call:

activityIndicator.stopAnimating()
see: https://luckypatcher.pro/
https://kodi.software/

https://showbox.software/

4 months ago ·
29827

if you want to implement a reusable spinner in Swift, I've found these tutorial: https://prograils.com/posts/reusable-activity-indicator-with-swift

4 months ago ·
30137

Hello friends my name is damyanti and i am a part time blogger who loves tech related stuff and always be try to give best to thier users you can visit here:
https://apktroll.com/blackmart/

about 1 month ago ·
30147

I want more code to add them on my website http://frackmanthemovie.com/

about 1 month ago ·
30159
Hinh xam hoa anh dao dep cho nam va nu 17

The new programming language changes towards development
http://bloxorzonline.com

about 1 month ago ·
30306
12 days ago ·
30344
9 days ago ·
30345
9 days ago ·