Creating programmatically a spinner (UIActivityIndicator)

May 8th, 2009 by Chris Leave a reply »

UIActivityIndicator commonly named Spinner is used usually to tell the user that some process is currently in progress.

There are three types of UIActivityIndicators:

activityindicators From the left: gray (UIActivityIndicatorViewStyleGray), white (UIActivityIndicatorViewStyleWhite) and large white (UIActivityIndicatorViewStyleWhiteLarge).

To allocate any of them use the following code:

	UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]];
	myIndicator.center = CGPointMake(160, 240);
	myIndicator.hidesWhenStopped = NO;

As you see this time you shouldn’t provide initWithFrame as long as large white style has got different dimensions then white (small) and gray. Instead, set its center point.

To add UIActivityIndicator to your view simple use addSubview method:

	[self.view addSubview:myIndicator];

By default, UIActivityIndicator displays a static image, what in most cases isn’t what you want. You can easily start and stop animating it by using two methods:

	[myIndicator startAnimating];
	[myIndicator stopAnimating]; 

Please note: the hidesWhenStopped=YES property causes the UIActivityIndicator disappear once you stopped animating it and appear again once you started animating.

If you need to know programatically what is the current state of UIActivityIndicator, you can use isAnimating property:

if ([myIndicator isAnimating]) NSLog(@"animating");
else NSLog(@"not animating");

UIActivityIndicator in status bar

spinnerstatusbarTo display a UIActivityIndicator use the UIApplication’s property: networkActivityIndicatorVisible.

	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

and to disable:

	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

That’s almost all about UIActivityIndicator. More can be found in UIActivityIndicator Class Reference in documentation.

Advertisement

10 comments

  1. Sorry, but I can’t agree with you. I do not like that approach. But it’s a great start. Perhaps with some far more information’s I may possibly be able to fully grasp it greater?

  2. Paula says:

    *PLEASE* release your memory… when you allocate it.

    Ugh.

  3. Excellent information here. This interesting post made me smile. Maybe if you throw in a couple of pics it will make the whole thing more interesting.

  4. Good topic for making an effective dissertation. . . . . .

  5. Very nice and helpful information has been given in this article. I like the way you explain the things. Keep posting. Thanks. . .

  6. Jill says:

    Hey! Thanks! Great code, expect for 1 thing.

    StartIt
    // my time consuming code
    StopIt

    Will *NEVER* work.
    You have to spawn a very complex series of code fragments to create a separate task.

    Why does something so simple… have to be so hard????

  7. Ronnie Reyne says:

    I am just curious what CMS your internet site is built on? It seems truly good and I like all the website visitor features that are offered. I’m sorry if this is the bad place to ask this then again I was not sure how to speak to you – with thanks.

  8. Any business owner who needs to bring more traffic to their business and grow their sales must create an article marketing strategy into their efforts. It has been proven to work over and over again by thousands of business owners all over the world. However, if not done correctly, you are not going to get the results you want. Therefore, it is very important to learn what not to do, as well. The greatest way to leverage your article marketing campaigns consists in usingan article spinning and submitter software so you can submit more articles quickly. If you submit to more article directories, you will gain more traffic, and unquestionably will help you to build high quality backlinks quickly.

  9. Chris says:

    Great article. It was very easy to follow and had me up and running in no time.

    There is one small error in your code though:

    UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]];

    should be

    UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    There is an extra “]” at the end of your example.

    Thanks again for the great article.

Leave a Reply