简体   繁体   中英

Animate UIView above all other views

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?

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM