Home > Software engineering >  ModalPresentationStyle fullscreen of UINavigationController doesn't work
ModalPresentationStyle fullscreen of UINavigationController doesn't work

Time:04-19

I have two ViewControllers in my application. One is actually present on the Storyboard, and it has one button to open another ViewController. The second ViewController is actually a WKWebView that I want to open on the button click. When I click the button the WebView just slides on top of the main ViewController but I want it to open in Full screen mode. So before navigating I did try to set modalPresentationStyle to full screen but it doesn't seem to work at all.

This is how I try to navigate to the WebViewViewController:

@IBAction func wkWebView(_ sender: Any) {
    if let url = URL(string: urlTextField.text ?? "https://www.google.com") {
        if (!urlTextField.isHidden) {
            urlTextField.endEditing(true)
        }
        let vc = WebViewViewController(url: url)
        let navVC = UINavigationController(rootViewController: vc)
        vc.modalPresentationStyle = .fullScreen;
        present(navVC, animated:true)
        }
}

This is the WebViewViewController code (I don't have it on my Storyboard, so I don't have any segue between the two ViewControllers ):

import UIKit
import WebKit

class WebViewViewController: UIViewController {

    private let webView: WKWebView = {
        let preferences = WKWebpagePreferences()
        preferences.allowsContentJavaScript  = true;
        let configurtions = WKWebViewConfiguration();
        configurtions.defaultWebpagePreferences = preferences;
        configurtions.allowsInlineMediaPlayback = true;
        configurtions.requiresUserActionForMediaPlayback = false
        let webView = WKWebView(frame: .zero, configuration: configurtions)
        webView.scrollView.contentInsetAdjustmentBehavior = .never
        return webView;
    }()
    
    private let url: URL;
    
    init(url: URL) {
        self.url = url
        super.init(nibName: nil, bundle: nil);
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        view.addSubview(webView);
        webView.load(URLRequest(url: url));
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        webView.frame = view.bounds
    }
}

How can I make the Webview ViewController to open in a fullscreen?

CodePudding user response:

You need to give .fullScreen to navigation controller not viewController like below.

    let vc = WebViewViewController(url: url)
    let navVC = UINavigationController(rootViewController: vc)
    navVC.modalPresentationStyle = .fullScreen;
    present(navVC, animated:true)
  • Related