Constrain a WKView to Safe Area

Hi,

I have created a simple WKView with a local webpage which is loaded.

I have a problem with display of the html document : I would like to constrain the view of the page to the Safe Area, and not to Super View. At this moment, when I scroll down, html page is visible at the top of the screen, in the status bar, which is not very beautiful on last iPhones (mix of text of my page and device hours/network status/battery status...).

I tested multiple solutions, in info file, but I am unable to suppress the transparency of this status bar or constrain webpage to fit only in Safe Area.

Could you help me ?

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
        }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let indexURL = Bundle.main.url(forResource: "index",
                                          withExtension: "html",
                                          subdirectory: "files") {
            self.webView.loadFileURL(indexURL,
                                     allowingReadAccessTo: indexURL)
                    }
        
    }}

Why don't you do it in storyboard ? That would certainly be easier.

Otherwise, just create constraints in code.

Hi,

Thans for your answer. I also tried to use main.storyboard, but I don't see where/how to implement this parameter.

Here is a pic of my problem. How to avoid html page extends to top bar when I scroll down ?

Constrain a WKView to Safe Area
 
 
Q