UINavigationItem titleView animation
Asked Answered
B

2

6

I am using a UIImageView-based animation to show a custom activity indicator. The following code is responsible for setting this :

+ (void)startLoadingActivity:(UIViewController *)vc {
    vc.parentViewController.navigationItem.titleView = [UIImageView imageViewWithPath:@"loading_" count:10 duration:1.3 frame:CGRectMake(0, 0, 22, 22)];
}

Here is a gist with the UIImageView+Animation category.

Whenever I am starting a web request, I am starting the activity indicator, and whenever the request is done, I am stopping it by setting the titleView property to nil.

The result is this:

OK

The request method is also called into the -viewDidAppear: method of the view controller.

However, whenever the animation starts during a transition, it seems like the position of the UIImageView is wrong.

The following result is showing the problem:

Please notice how the activity indicator is showing on top of the "Groups" button.

KO

Where is this problem coming from? How can I solve it?

Benedick answered 12/4, 2015 at 18:52 Comment(0)
C
0

I think the problem is that although your setting your titleView to nil, you do not remove the loader from the view, so it's frame goes to (0,0) position.

Try the following:

+ (void)startLoadingActivity:(UIViewController *)vc {
    UIImageView *imageView = [UIImageView imageViewWithPath:@"loading_" count:10 duration:1.3 frame:CGRectMake(0, 0, 22, 22)];
    imageView.tag = 999;
    vc.parentViewController.navigationItem.titleView = imageView;
}

And whenever setting the

titleView = nil

Also do

[[self.navigationController.view viewWithTag:999] removeFromSuperView];
Countess answered 12/4, 2015 at 19:20 Comment(3)
I tried and it doesn't seem to be that. The problem occurs when the loader is added.Benedick
I've edited my answer to remove to view from the navigation controller. Does it help?Countess
No, it didn't change anything. Thanks anyway.Benedick
D
0

This question is really nice demo of the issue, and the answers are here: Stackoverflow title moves while push/pop of viewcontroller

Long story short:

  1. Use UIView with .autoresizingMask = UIViewAutoresizingFlexibleTopMargin
  2. Don't do it in "viewWillAppear:" (do in viewDidLoad / update in DidAppear or later )
Dendrite answered 7/8, 2017 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.