2

I am using https://github.com/ali-ehsan/GIFProgressHUD as a loading spinner across my app but it seems to only be attached to a single view, so if I'm loading from a login screen to the home screen, during the transition this progress hud will disappear automatically.

Is there a way I can get a view to stay between screens kinda like a activity indicator ?

I'm developing in Objective-C.

D34thSt4lker
  • 447
  • 1
  • 5
  • 12

1 Answers1

1

Add (attach) your progress view to the main window of the application instead of a subview of the controller or view that is being removed when you transition.

A - use KVC

id appDelegate = [UIApplication sharedApplication].delegate;
UIWindow *keyWindow = [appDelegate valueForKey:@"window"];

B - use keywindow property of UIApplication

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

This property holds the UIWindow object that is most recently sent the makeKeyAndVisible message.

C - use windows property of UIApplication

UIWindow *keyWindow = [[UIApplication sharedApplication] windows].lastObject;

Suggestion: just tag your progress view so you can easily find it later when you need to remove it.

UPDATE: Now that you have the main window (the top most view in the hierarchy), you can add any type of stack object you like (a view, label, activity indicator, etc.) just like you would to any UIView.

// Easiest and most readable way to get the key window
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; 

UIView *testview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
testview.backgroundColor = [UIColor redColor];
[keyWindow addSubview:testview];

testview.center = keyWindow.center;

UPDATE 2:

As a last resort if you are navigating in ways outside of traditional UIViewController transitions and presentations, you can always add a new window to the stack as seen here in this SO post.

If the issue of the disappearing view persists you might want to try changing the way in which you navigate through your views / controllers OR pass references between the controllers to allow this "disappearing" view to live on.

Another reference regarding this type of navigation.

Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42
  • This doesnt work. I know how to get the top window. The problem is that while transitioning between controllers, the previous top controller that had the "spinner" is gone, so it disappears – D34thSt4lker Jan 19 '18 at 17:28
  • Of course it does.. along with the now previous controller. You can't expect the spinner to stay around once it's parent is removed. Find another way to attach the spinner to something that *doesn't get removed* when you transition - hence my top most window suggestion – Will Von Ullrich Jan 19 '18 at 17:34
  • I obviously don't know how to attach it to the window then. I tried using option B. Maybe I'm doing it wrong? – D34thSt4lker Jan 19 '18 at 17:35
  • No problem. Using the key window is a nice way to allow content to stay put during things such as controller transitions. Test it out with some controller navigation and you can see how it stays on top above your other content. – Will Von Ullrich Jan 19 '18 at 17:42
  • Could this not be working because I am switching the root view controller? – D34thSt4lker Jan 19 '18 at 17:49
  • I think I was able to use that window snippet however I think we moved towards a different approach for these spinners. Thanks for your help! – D34thSt4lker Jan 26 '18 at 06:33
  • No problem glad you got it working - if you felt this solved your issue if you could accept the answer please. Either way happy coding! – Will Von Ullrich Jan 26 '18 at 15:52