Nova Togatorop
Nova Togatorop

Reputation: 29

Webview shows up in status bar - Xcode

I'm new to xcode. I try to build my ios app from my existing webapp that I build with Ruby on Rails.

I use the WKWebView class to embed web content in my app. This is my ViewController.swift :

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()
        
        let myURL = URL(string:"https://www.myapp.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }}

When I run simulator, It turns out I can see the content in the status bar when I scroll up.

enter image description here

How can I hide the content in the status bar?

Upvotes: 1

Views: 1977

Answers (1)

Daniel Barden
Daniel Barden

Reputation: 1024

This is happening due to the view = webView line. Ideally, you should add the web view as a subview. Then, you should configure the auto layout constraints on the webview to be pin the constraints of your web view to the superview.

A quick proposal would be like this (I added everything on viewDidLoad, since the view should already be loaded before we start adding view to it):

    override func viewDidLoad() {
        super.viewDidLoad()

        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)

        // [1] Since we're using auto layout, this ignores the frame and
        // considers only the auto-layout constraints
        webView.translatesAutoresizingMaskIntoConstraints = false

        let myURL = URL(string:"https://www.google.com")
        let myRequest = URLRequest(url: myURL!)

        // [2] Adds the webview as a subview of the view
        view.addSubview(webView)
        webView.load(myRequest)

        // [3] Pins the webview to the safe area layout guides.
        // From the documentation: When the view is visible onscreen, this guide
        // reflects the portion of the view that is not covered by navigation bars,
        // tab bars, toolbars, and other ancestor views.
        // https://developer.apple.com/documentation/uikit/uiview/2891102-safearealayoutguide
        NSLayoutConstraint.activate([
            webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        ])
    }

Upvotes: 1

Related Questions