Pages

Wednesday, May 11, 2011

[Code Snippet] UIActivitiyIndicator at right bar button place.

Let's continue with the weekly code snippet series. Sometimes we have an UITableViewController inside a UINavigationController that we want to update performing the data query in background and notify the user that this update is going on with a UIActivityIndicator control.

The better place to place the UIActivity indicator is on navigation bar, so the activity indicator don't appears in the middle of the screen bothering the user in is normal interaction with the application.

To accomplish this, here you are a little code snipe for placing an little UIActivityIndicator at top right corner of the screen instead of a UIBarButton.

// Create a 'right hand button' that is a activity Indicator
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
self.activityIndicator = [[UIActivityIndicatorView alloc]
            initWithFrame:frame];
[self.activityIndicator sizeToFit];
self.activityIndicator.autoresizingMask =
    (UIViewAutoresizingFlexibleLeftMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleBottomMargin);

UIBarButtonItem *loadingView = [[UIBarButtonItem alloc]
         initWithCustomView:self.activityIndicator];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;

I've found that code snippet in Even Davey's Blog

Enjoy it!

2 comments:

  1. Hey guys, i just wanted you to know that I really love your blog and your weekly updates!!!
    Keep up with your good work!
    Thanks for what you've done so far :)
    Max

    ReplyDelete
  2. Thanks for your support, Max. We'll try to keep this blog alive and updated every week.

    Regards!
    Sergio

    ReplyDelete