iPhone XR中的WKWebView重叠状态栏

时间:2020-07-06 17:32:50

标签: ios swift

我的应用程序中有一个WKWebView视图,该视图呈现了在Ionic框架中开发的内容。似乎除X型号(状态栏较高)以外的所有Apple设备在不重叠状态栏的情况下均表现良好。但是“ X”模型有问题。

我将其约束设置为:

Interface builder constraints image

但是,结果是没有强制执行这些约束,当模式窗口显示时,它超出了iPhone XR,X,XS模拟器和物理设备中的状态栏边界,从而几乎无法访问“退出”按钮以关闭模式窗口。

App displaying issue

这可能是Ionic代码本身的问题,还是像我不得不以某种方式在XCode中进行修复?

我看过: UIWebView show overlapping status bar in ios-11, iPhone-X, Xcode-9

但是似乎无法通过编程方式调整视图不起作用,因为我试图将此代码添加到我的ViewController的loadView()和viewDidLoad()中无济于事(尝试查找框架时,我得到了ERR BAD ACCESS以相对于StatusBar调整视图的高度)-iOS 13,Swift 5:

override func loadView() {
    
    let webConfiguration = WKWebViewConfiguration()
    
    #if false
    
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    #else
    
    //First, let's find out the height of the status bar, so we don't invade it.
    let winScene = UIApplication.shared
                    .connectedScenes
                    .first
    
    let windowScene = winScene as! UIWindowScene
    
    let sbHeight = windowScene.statusBarManager?.statusBarFrame.height
    let heightTotal = view.frame.height + sbHeight!
    
    webView = WKWebView(frame: CGRect( x: 0, y: heightTotal, width: view.frame.width, height: view.frame.height - sbHeight!), configuration: webConfiguration )

    #endif
    
    webView.uiDelegate = self
    view = webView
    
       }

用尽了所有想法,因此不胜感激。

1 个答案:

答案 0 :(得分:1)

添加这样的约束:

enter image description here

在ViewController中使用以下代码:

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myURL = URL(string:"https://www.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

所以请记住添加WebKit框架:

enter image description here enter image description here enter image description here

这样,我可以使用iPhone 11 Pro模拟器正确获取视图。

enter image description here