Issues with iOS 6 and UIActivityIndicator in UINavigationBar titleView

having problems with this since iOS 6. I can’t understand what has changed, which will lead to this behavior. This worked fine in 5. Now the activity indicator does not appear in a timely manner or at all. Any help is appreciated.

-(void)myMethod
{
    UIView *currentTitleView = [[self navigationItem] titleView];


    // Create an activity indicator and start it spinning in the nav bar
    UIActivityIndicatorView *aiview = [[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    [[self navigationItem] setTitleView:aiview];
    [aiview startAnimating];

    // Start of Block code
    void (^block)(arg1, arg2) =
    ^(arg1, arg2)
    {
       block code;
       [aiview stopAnimating];
       [[self navigationItem] setTitleView:currentTitleView];
    };
// End of Block code




}
+5
source share
1 answer

It looks like it myMethodis being called from a background thread. As a rule, all interactions with UIKit elements (including UIActivityIndicatorView) should always be performed in the main thread. Try using GCD to move the user interface code to the main queue (main aka thread).

-(void)myMethod {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *currentTitleView = [[self navigationItem] titleView];

        // Create an activity indicator and start it spinning in the nav bar
        UIActivityIndicatorView *aiview = [[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

        [[self navigationItem] setTitleView:aiview];
        [aiview startAnimating];
    });

    // Start of Block code
    void (^block)(arg1, arg2) =
    ^(arg1, arg2)
    {
       block code;

        dispatch_async(dispatch_get_main_queue(), ^{
           [aiview stopAnimating];
           [[self navigationItem] setTitleView:currentTitleView];
        });
    };
// End of Block code
}
0
source

All Articles