Reputation: 999
I want to display/animate a UIView above the whole content of my app. The following approach works, but is not a very good idea to attach everything to the window.
[[[[UIApplication sharedApplication] delegate] window] addSubview:view]
What is a good alternative?
Upvotes: 0
Views: 336
Reputation: 307
I think you can try visibleViewController method to get the top viewController
//Try this to get the top viewController
UIViewController *vc = [app.rootViewController visibleViewController];
// After getting the top viewController, and then add your subview to it
[vc addSubView:view];
// and try bringing this view to the top
[vc bringSubViewTo....(fogot~):view];
see if it could help :)
Upvotes: 1
Reputation: 25001
I think the approach is correct, you do need to attach it to the window, as you may not know what the view hierarchy is.
What you can do to tidy up the code a little bit, is encapsulate that logic into some methods accessible through the AppDelegate
or something:
- (void)addAlwaysVisibleView:(UIView *)topmostView;
- (void)removeAlwaysVisibleView;
The AppDelegate
can then keep the state, and know if an "always visible" view is already attach, to keep things in order and do some housecleaning when needed.
Upvotes: 0