Make this view appear in every view, inside the app

梦想的初衷 提交于 2019-12-25 01:45:16

问题


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:


  1. Create a custom UIView (let's call it curtainView).
  2. In whatever init method you plan to use for your views (initWithCoder: if they are loaded from XIBs or StoryBoard) put the code for creating the 'curtain' subview.
  3. In this custom curtainView class, add a property for referencing the curtain subview.
  4. Make all of your UIViews (NOT UIViewControllers) subclasses of the curtainView.
  5. Voila!


来源:https://stackoverflow.com/questions/17165469/make-this-view-appear-in-every-view-inside-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!