问题
This is a "curtain" hanging down from my navigationbar.
How can I have make this view as an overlay on all views in my app? I must be able to add images to this overlay's subviews
回答1:
Simply add it to your application's key window. That will overlay everything.
[[[UIApplication sharedApplication] keyWindow] addSubview:curtainView];
I use this method to present a full-screen progress indicator overlay (a singleton actually) to block the UI during some activities, regardless of context.
Please note that a UIWindow have no interface orientation, it is always aligned with the device, so rotating the UI is something to you have to handle if going this way (this actually indicates that this is not the best design).
回答2:
I think the best way to get the effect you're looking for is to create your own custom container view controller. That'll let you host other view controllers as children of your custom view controller, so you can have a single "curtain" that's on screen all the time even as the content underneath it changes.
Some of the advantages to a custom view controller compared to the other solutions are:
- clean design: none of the other views or view controllers in the app need to worry about the curtain
- single instance: the curtain will stay in place during view controller transitions
- DRY: seems to fit nicely with the "don't repeat yourself" concept
回答3:
You can achieve required with following steps --
1.) Create extension of UINavigationController for eg. UINavigationControllerWithCurtain.
2.) Override initWithCoder if you are constructing navigation with xib or else initWithRootController and add custom view of your curtain over navigationcontroller's view i.e. self.view itself in constructors.
3.) You need to override pushViewController method in order to set your custom curtain view at the top most layer so add following --
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super viewController:viewController animated:animated];
[self.view bringSubviewToFront:customCurtainView];
}
回答4:
- Create a custom
UIView(let's call itcurtainView). - In whatever
initmethod you plan to use for your views (initWithCoder:if they are loaded from XIBs or StoryBoard) put the code for creating the 'curtain' subview. - In this custom
curtainViewclass, add a property for referencing the curtain subview. - Make all of your
UIViews (NOTUIViewControllers) subclasses of thecurtainView. - Voila!
来源:https://stackoverflow.com/questions/17165469/make-this-view-appear-in-every-view-inside-the-app