简体   繁体   中英

Present UIWebview on top on currently displayed view controller

I'm trying to modally display a UIWebview on top of the currently displayed view controller. The code to popup the webview lives in a static library and has no idea about the view controller hierarchy of the app that it lives in. The code below works for my test cases, but not sure if it would work for all possible view controller hierarchies.

UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
UIViewController *presentedVC = [rootViewController presentedViewController];
if (presentedVC) {
    // We have a modally displayed viewcontroller on top of rootViewController.
    if (!presentedVC.isBeingPresented) {
    [presentedVC presentViewController:vc animated:YES completion:^{
        //
    }];
} else {
    rootViewController presentViewController:vc animated:YES completion:^{
        //
    }];
}

Does the presentedViewController always give the currently visible view controller?

If what you want to do is to display the UIWebView modally on top of whatever View Controller is displayed in your app, you don't need the "topmost" one. Getting the Root View Controller will suffice. I do this all the time whenever I want to present a View on top of everything else. You only need a reference to the root view controller in your library:

self.rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

Having this reference you have two options now:

The first one is to use UIViewController 's method presentViewController:animated:completion: to display another View Controller which will contain your UIWebView

The second option is to fake the modal view controller by adding a subview that covers the entire screen, has a (semi)transparent background, and contains the view you want to display "modally". Here is an example:

@interface FakeModalView : UIView // the *.h file

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIView *background; // this will cover the entire screen

-(void)show; // this will show the fake modal view
-(void)close; // this will close the fake modal view

@end

@interface FakeModalView () // the *.m file

@property (nonatomic, retain) UIViewController *rootVC; 

@end

@implementation FakeViewController
@synthesize webView = _webView;
@synthesize background = _background;
@synthesize rootVC = _rootVC;

-(id)init {
    self = [super init];
    if (self) {

        [self setBackgroundColor: [UIColor clearColor]]; 
        _rootVC = self.rootVC = [[[[[UIApplication sharedApplication] delegate] window] rootViewController] retain];
        self.frame = _rootVC.view.bounds; // to make this view the same size as the application

        _background = [[UIView alloc] initWithFrame:self.bounds];
        [_background setBackgroundColor:[UIColor blackColor]];
        [_background setOpaque:NO];
        [_background setAlpha:0.7]; // make the background semi-transparent

        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(THE_POSITION_YOU_WANT_IT_IN)];

        [self addSubview:_background]; // add the background
        [self addSubview:_webView]; // add the web view on top of it
    }
    return self;
}

-(void)dealloc { // remember to release everything

    [_webView release];
    [_background release];
    [_rootVC release];

    [super dealloc];
}

-(void)show {

    [self.rootVC.view addSubview:self]; // show the fake modal view
}

-(void)close {

    [self removeFromSuperview]; // hide the fake modal view
}

@end

If you have any other questions just let me know.

Hope this helps!

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