ios - 在当前显示的 View Controller 顶部呈现 UIWebview

标签 ios cocoa-touch uiviewcontroller uikit

我正在尝试在当前显示的 View Controller 之上以模态方式显示 UIWebview。弹出 webview 的代码位于静态库中,并且不知道它所在的应用程序的 View Controller 层次结构。下面的代码适用于我的测试用例,但不确定它是否适用于所有可能的 View Controller 层次结构.

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:^{
        //
    }];
}

presentedViewController 是否总是提供当前可见的 View Controller ?

最佳答案

如果您想要做的是在应用程序中显示的任何 View Controller 之上以模态方式显示 UIWebView,则不需要“最顶层” Controller 。获取 Root View Controller 就足够了。每当我想在其他所有内容之上呈现一个 View 时,我总是这样做。 您只需要引用库中的 Root View Controller :

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

有了这个引用资料,您现在有两个选择:

第一个是使用 UIViewController 的方法 presentViewController:animated:completion: 显示另一个包含您的 UIWebView 的 View Controller

第二个选项是通过添加一个覆盖整个屏幕、具有(半)透明背景并包含要“模态”显示的 View 的 subview 来伪造模态视图 Controller 。这是一个例子:

@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

如果您有任何其他问题,请告诉我。

希望这有帮助!

关于ios - 在当前显示的 View Controller 顶部呈现 UIWebview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863063/

相关文章:

iOS - 计算滑动次数

ios - UIViewController 中的 UITableView

ios - CKFetchRecordChangesOperation-更多即将到来

ios - Xcode 9 iOS模拟器-无法获得有效的进程句柄

ios - AVCam 保存全屏捕获的图像

iphone - 如何处理 UILabel 上的触摸事件作为 UITableViewCell 的 subview ?

android - 执行期间适当的应用崩溃(将iOS应用转换为Android)

ios - adddependency 方法在 NSOperationQueue 中的工作原理

iphone - 如何在 Tab Bar + NavigationControllers 应用程序中启用横向?

objective-c - 从 Obj C didSelectionRow 调用 Swift ViewController