IOS Hybrid App-避开iPhone 11的安全区域

时间:2019-11-01 13:44:55

标签: iphone wkwebview

对于我的IOS混合应用程序,我以编程方式创建WKWebView,如下所示

from tstats import Spelare

def filereader(filename):
    with open(filename) as f:
        y = []
        for row in f:
            playerdata = rad.split(',')
            player = Spelare(playerdata[0], float(playerdata[1]), int(playerdata[2]),
                             int(playerdata[3]))
            y.append(player)

    return y

def main():
    y=filereader('file.txt')
    y.sort(reverse=True)
    for individual in y:
        print(individual)
    n1 = int(input("Välj spelare 1"))
    n2 = int(input("Vlj spelare 2"))

    name1 = int(n1 - 1)  #bc 0 index
    name2 = int(n2-1)
    y[name1].nrplayed()
    y[name2].nrplayed()

    winner = int(input("Name winner"))
    if winner == name1:
        y[name1].winningplayer()
    elif winner == name2:
        y[name2].winningplayer()

    y.sort(reverse=True)
    for individual in y:
        print(individual)


main()

当设备旋转时,我还会更改Web视图的大小:

- (void)viewDidLoad
    {
    [super viewDidLoad];

    _statBarH = [UIApplication sharedApplication].statusBarFrame.size.height ;
    appH = self.view.frame.size.height ;
    appW = self.view.frame.size.width ;
    webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:theConfiguration];
    smallScreen = [deviceType hasPrefix:@"iPhone"];
    if (smallScreen && appW > appH) {webView.frame = CGRectMake(0, 0 , appW, appH);}
    else                            {webView.frame = CGRectMake(0, _statBarH, appW, appH - _statBarH);};
    };

javascript函数IOSOrientationChanged仅以纵向或横向格式重新绘制我的应用程序内容,并从javascript window.innerWidth和window.innerHeight中检测新的高度和宽度

这种方法对我来说效果很好,直到Apple移除了iPhone X 11上的主页按钮...

在这些较新的电话上发生的事情(至少在XCode 11 IOS 13模拟器中,因为我实在无法负担得起)是,当首次创建Web视图时,它尊重上方的安全区域,却不尊重下方的安全区域安全区域。

但是,当我在模拟器中旋转设备时,Web视图同时尊重两个安全区域。

对于iPhone 11,变量appW和appH在纵向中为414和896,在横向中为896和414。变量_statBarH的纵向为44,横向为0。

但是,在网络视图中,纵向中的window.innerHeight最初设置为852(896-44),但在旋转后将其设置为838。

请有人能告诉我我做错了吗?某些内部视图约束不是第一次实施,而是在轮换之后实施吗?

1 个答案:

答案 0 :(得分:0)

解决方案是双重的

  1. 首先在viewDidAppear中而不是在viewDidLoad中创建WKWebView,因为直到viewDidAppear才可以以编程方式访问safeAreaInserts值

  2. 其次,创建WKWebView框架时,请使用safeAreaInserts中的值,如下所示

    smallScreen = [deviceType hasPrefix:@"iPhone"];
    topMargin   = [UIApplication sharedApplication].statusBarFrame.size.height ;
    if (@available(iOS 11, *)) topMargin = self.view.safeAreaInsets.top ;
    bottomMargin = 0 ;
    if (@available(iOS 11, *)) bottomMargin = self.view.safeAreaInsets.bottom ;
    
    int top    = (smallScreen && appW > appH)? 0    : topMargin ;
    int height = (smallScreen && appW > appH)? appH : appH - topMargin - bottomMargin ;
    NSLog (@"Top %d, Height %d",top,height);
    
    webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, top , appW, height) configuration:theConfiguration];