【问题标题】:Cordova/PhoneGap iOS: WebView becoming shorter after taking a picture or opening InAppBrowserCordova/PhoneGap iOS:拍照或打开 InAppBrowser 后 WebView 变短
【发布时间】:2024-01-20 18:32:01
【问题描述】:

当我用相机插件拍照时,整个 WebView 变短了。这就像屏幕被从底部吃掉状态栏的高度 - 20px。它根本与拍照无关,如果我只打开画廊然后关闭它也会发生。如果我打开 InAppBrowser 然后关闭它,也会发生同样的事情。这是一个例子:

这是选择照片之前的样子

然后出现照片选择对话框(不管是模拟器,在真机上都一样)

然后当我关闭对话框时会发生这种情况 - 查看屏幕底部

如果我继续添加照片,每次屏幕被“吃掉”20px

我发现window.innerHeight 减少了20px,所以它与状态栏有关。我怎样才能解决这个问题?

【问题讨论】:

    标签: ios cordova webview


    【解决方案1】:

    我在另一个线程中偶然发现了答案。这里是:

    在 MainViewController.h 中:

    @interface MainViewController : CDVViewController
    @property (atomic) BOOL viewSizeChanged;
    @end
    

    在 MainViewController.m 中:

    @implementation MainViewController
    
    @synthesize viewSizeChanged;
    
    [...]
    
    - (id)init
    {
        self = [super init];
        if (self) {
            // On init, size has not yet been changed
            self.viewSizeChanged = NO;
            // Uncomment to override the CDVCommandDelegateImpl used
            // _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
            // Uncomment to override the CDVCommandQueue used
            // _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
        }
        return self;
    }
    
    [...]
    
    - (void)viewWillAppear:(BOOL)animated
    {
        // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
        // you can do so here.
        // Lower screen 20px on ios 7 if not already done
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && !self.viewSizeChanged) {
            CGRect viewBounds = [self.webView bounds];
            viewBounds.origin.y = 20;
            viewBounds.size.height = viewBounds.size.height - 20;
            self.webView.frame = viewBounds;
            self.viewSizeChanged = YES;
        }
        [super viewWillAppear:animated];
    }
    

    来源:https://*.com/a/19407903/1555838

    【讨论】: