su1t1a
Last Updated: June 22, 2019
·
102.6K
· 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

82 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

over 1 year 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/

11 months ago ·
30147

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

11 months ago ·
30306
30344
10 months ago ·
30450

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

10 months ago ·
30505

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

10 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

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

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

8 months ago ·
30876

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

8 months ago ·
30919
8 months ago ·
31003
8 months ago ·
31008

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

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

8 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.

8 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

8 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/.

8 months ago ·
31133

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

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

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

7 months ago ·
31273

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

7 months ago ·
31326

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

7 months ago ·
31334
7 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/

7 months ago ·
31379

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

7 months ago ·
31455

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

6 months ago ·
31465
31500

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

6 months ago ·
31501

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

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

6 months 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/

6 months ago ·
31547
6 months 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

5 months ago ·
31707
31795

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

5 months ago ·
31811
31872
31890
31900
31914

The best Android apps

4 months ago ·
31979

This is good piece of writing and pleasant urging commented
here, I am really enjoying by these.

4 months ago ·
31990

Really awesome dear great one.

https://firestickapps.com/

4 months ago ·
32041

It helped me a lot, i was looking for that code. https://myboyapkz.com

4 months ago ·
32106
32129
3 months ago ·
32166

http://tilmanification.org/ is for Instagram Private Viewing

3 months ago ·
32191

Thank you for the post.
https://livenettvtips.com/

3 months ago ·
32225
32345

Your association aptitudes are brilliant I supported it in fact and I have a surprising time thing for you to check your impeccability https://ricepuritytest.us/rice-purity-test-score-scales

2 months ago ·
32384

Excellent post. I absolutely love this website. Continue the good work! -

2 months ago ·
32503

Lovely platform for safely share files mobile to mobile is shareit and its work 24/7.
https://shareitforpcs.online

about 2 months ago ·
32587

Really Very Nice Article Keep It Up Dear - Also Check https://asphalt-8-mod-apk.info/

about 2 months ago ·
32597

That's a great article. Thanks for sharing it with us!
https://techbreach.co/kickass-proxy-mirror-sites/2003/

about 2 months ago ·
32740

Good content shared here. Thanks for this type of amazing content. https://technopo.com/

about 1 month ago ·
32745

Showbox is a free entertainment app which now considered the best in the earth. It is useable for all kind of humanoid devices ilk HTC, Samsung, LG, Toilet, etc. That is why it is exceedingly pop in public. https://playboxmovies.com/showbox-apk-download/

about 1 month ago ·
32751
about 1 month ago ·
32888
about 1 month ago ·
32910

How many realy this blog https://www.netter.id/

about 1 month ago ·
32943

hello, everyone, this information is very good and I hope you all like it. https://workspaceloginn.com/

about 1 month ago ·
33007

The Earth is still rotating, let's love http://geometrydash-game.com -> geometry dash http://geometrydash-game.com/tags/basketball-legends basketball legends

28 days ago ·
33052

AppVN is an outsider application store for Android. Not at all like Google Play, this application store furnishes you with some extraordinary highlights, for example, the office to download certain official premium applications for nothing.
https://www.appvn.one/

28 days ago ·
33195

fmovies - Free movies online, here you can watch movies online in high quality for free without annoying of advertising, just come and enjoy your movies online. https://showboxdownloadmovies.com/fmovies/

9 days ago ·
33203

123Movies4u
123Movies4u is another popular telecasting streaming site to ticker videos without downloading. You wicker wake all the pic and TV series capacity without registration.
On 123movies4u, you can determination movies according to genres. The nearly popular pic genres are activity, clowning, romanticism, play, thriller, sci-fi, etc.
If you are quieten notable able-bodied to obtain the flick, so you wicker attend A-Z leaning to beholding movies according to starting alphabets. You wicker get A-Z inclination features altogether the pop flick cyclosis sites ilk YesMovies, Solarmovies, Fmovies, etc. https://moviesapk.org/123movies/

7 days ago ·
33225

How To Picket Movies Online?
More than that, if you accession any on-line movie portals, then you are boost served with about additional features excursus from movie observance. This plainly agency that you can besides sentinel the latest genius interviews, film trailers, online booking, and demonstrate timings too. When you opt for a picture rubric concluded a minimum defrayment from a really foresightful mind of categories, you testament able to pass the column reviews of detail movies premature to purchasing. https://showboxdownloadmovies.com/movierulz/

7 days ago ·
33227

Watch online free HD movies. On gostream moves anyone can watch latest movies and daily tv series online without registration.123 Movies Free place for .. https://showboxdownloadmovies.com/123movies/

7 days ago ·
33228

SolarMovie is an excellent add-on if you're looking to extend the functionality of Kodi. It offers access to the latest blockbusters plus the classics! https://showboxdownloadmovies.com/solarmovie/

7 days ago ·
33268

this site is very informative thanks for sharing them resources
https://www.opentechinfo.com/showbox-android/
https://www.opentechinfo.com/gbwhatsapp/

7 days ago ·
33306

you have to great information shared watch plz, these links.
https://clashofmagic.xyz/clash-of-clans-hack/
https://clashofmagic.xyz/clash-of-magic-s2-apk/

7 days ago ·
33531

well the new version is https://freefireapk.info/

5 days ago ·
33805

If you are a Android user and you want to get all kinds of modified,tweaked applications,then you need a single app which enables you to get all paid apps free. https://getapkmarket.xyz/download

3 days ago ·
33812

GREAT POST ADMIN THANKS FOR THIS REALLY WORTHY, pubg for pc free download guide.
https://www.pubgforpcfreedownload.com/

2 days ago ·
33818