su1t1a
Last Updated: February 18, 2019
·
93.44K
· itseranga

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

49 Responses
Add your response

16862

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

over 1 year ago ·
20714

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

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 ·
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

9 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/

7 months ago ·
30147

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

7 months ago ·
30306
30344
6 months ago ·
30450

customActivityIndicatory(self.view)
or
customActivityIndicatory(self.view, startAnimate: true)
//hide activityIndicatorView
customActivityIndicatory(self.view, startAnimate: false)
Check
https://www.gbwhatsapp.biz/

6 months ago ·
30505

Working Fine,
Thankss.
http://appvn.io/

6 months ago ·
30777

<a href="http://militarydietsubstitutes.com/">military diet substitutes</a>

Have you been having difficulty losing those few added pounds? Perhaps you have tried other weight loss plans and have not seen the results you’re searching for military diet substitutes

5 months ago ·
30811

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()
https://redboxtv.co/

5 months ago ·
30875

How to use:

//Show activityIndicatorView
customActivityIndicatory(self.view)
or
customActivityIndicatory(self.view, startAnimate: true)
//hide activityIndicatorView
customActivityIndicatory(self.view, startAnimate: false)

https://www.redboxtv.me/
https://www.cloudtvapk.net/
https://www.videoder-app.com/

4 months ago ·
30876

well it was very helpful thanks for sharing https://www.redboxtv.me/

4 months ago ·
30919
4 months ago ·
30955

You can fine more thips about thie sapp on our site https://iamapk.com/tvtap/

4 months ago ·
31003
4 months ago ·
31008

download CotoMovies for watching movies and tv shows online for free.

4 months ago ·
31014

how to use this,
How to use:

//Show activityIndicatorView
customActivityIndicatory(self.view)
or
customActivityIndicatory(self.view, startAnimate: true)
//hide activityIndicatorView
customActivityIndicatory(self.view, startAnimate: false)
Redbox TV APK http://whirlybirdbrooklyn.com/redbox-tv-apk/

4 months ago ·
31021

I was just making this small mistake. I just realized it seeing this article. Forgot to call actInd function. thanks for sharing man. You really saved me. Appflix to watch netflix for free in your free time.

4 months ago ·
31026

It doesn't show in the centre. It show bit blow to the centre. Any ideas please?
https://www.showbox.buzz
https://www.cartoonhds.com
https://www.mobdros.com

4 months ago ·
31075

The source code is beneficial, thanks for sharing this information with us. Also, you can get the unique source of the tvtap from the https://tvtap.app/.

4 months ago ·
31133

Thanks for sharing, it proved beneficial for me on my site http://www.hotelsassemble.com/

3 months ago ·
31209
31225

The swift code which you have given is useful, thanks for sharing with us, you can get such amazing lectures on the tvtap pro app, here is the official link https://tvtapnet.download/.

3 months ago ·
31233

Those source codes help me a lot to develop my ios sites and apps. Recently terrarium tv for ios movie app working fine with your steps. Hope you like it https://terrariumtv.mobi/terrarium-tv-ios-iphone/

3 months ago ·
31273

nice tip i loved it, http://www.noamdover.com/

3 months ago ·
31326

Click here for best alternatives for terrarium tv for android devices..
http://titaniumtv.me/download-titanium-tv-for-android/

about 2 months ago ·
31334
about 2 months ago ·
31344

get the apk file from https://www.tvtap.co/

about 2 months ago ·
31352

That was awesome article dear. keep going.
get your best streaming app for iPhone HERE

http://cyberflixtv.me/cyberflix-tv-app-install-on-firestick-android-tv/

about 2 months ago ·
31379

Ios customised activity indicator was very useful for me
https://topbest10review.com

about 2 months ago ·
31455

thanks for sharing the information, https://vasundhara-property.xyz/flats-in-vasundhara/

about 2 months ago ·
31465
about 2 months ago ·
31500

This site was... how do I say it? Relevant!! Finally I have found something that helped me. Cheers!

28 days ago ·
31501

<a href='https://ricepuritytest.info/'>Rice Purity Test</a>

[url=https://ricepuritytest.info/]Rice Purity Test[/url]

28 days ago ·
31517

This works too! I ran into an issue because I used a UITABLEViewController instead of a UIViewController. And the touchesBegan was never fired because that method applies to the CONTROLLER and not the TABLEVIEW that was sitting on top of my controller. I had to add a gesture recognizer in order for it to hide my loader on tap. Great Info.http://luckypatcherdownloads.org/

28 days ago ·
31547
28 days ago ·
31613
31650
31656
31671

Thank you so much for this article. I like it so much. It is very helpful https://showbox-apk.mobi

28 days ago ·
31707
31795

Tutuapp for android download link https://tutuappvip.mobi

7 days ago ·
31811
31872
31890
31900
about 1 hour ago ·