UiWebView全屏显示

问题描述 投票:0回答:1

下面的代码执行一个包含UiWebView的UiViewController的显示,当UiWebView显示为下面的图像时,必须全屏显示UiWebView,我以所有已知的方式尝试通过修改情节提要,通过快速编程,在下面,您可以找到视图代码和源存储库,您可以解释如何将此ui Webview置于全屏状态,我是否还要附加模拟器图像?

Image of screen

Repository Link

ViewController.swift

override func viewDidLoad() {

    super.viewDidLoad()
    let controller = BrowserViewController()
    let navigationController = UINavigationController(rootViewController: controller)
    navigationController.modalPresentationStyle = .overFullScreen
    self.navigationController?.present(navigationController, animated: true, completion: nil)


}

BrowserViewController.swift

import UIKit

func hexStringToRGB(_ hexString: String) -> (red: CGFloat, green: CGFloat, blue: CGFloat) {
    var cString: String = hexString.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

    if (cString.hasPrefix("#")) {
        cString.remove(at: cString.startIndex)
    }

    if ((cString.count) != 6) {
        return (red: 0.0, green: 0.0, blue: 0.0)
    }

    var rgbValue: UInt32 = 0
    Scanner(string: cString).scanHexInt32(&rgbValue)

    return (
        red: CGFloat((rgbValue & 0xFF0000) >> 16),
        green: CGFloat((rgbValue & 0x00FF00) >> 8),
        blue: CGFloat(rgbValue & 0x0000FF))
}

class BrowserViewController: UIViewController, UIWebViewDelegate {


    //Array che contiene gli url che hanno il permesso di essere visualizzati nella UiWebView
    let urlPermessi = ["web.parktogo.it", "m.facebook.com", "www.facebook.com", "paypal.com", "accounts.google.com"]


    //Definizione della UIWebView
    var myWebView: UIWebView = UIWebView()
    let button = UIButton(type: UIButton.ButtonType.custom)
    //Definizione url home-page
    let urlhomepage = URL (string: "https://web.parktogo.it")
      let dictionatyUnclock = NSDictionary(object: "mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11a501 safari/9537.53", forKey: "UserAgent" as NSCopying)


    //Check url
    func checkUrl(url: String) {
        if(url == "web.parktogo.it" || url == "https://web.parktogo.it") {
            self.button.isHidden = true
            UserDefaults.standard.register(defaults: dictionatyUnclock as! [String: Any])
        }
        else {
            UserDefaults.standard.register(defaults: dictionatyUnclock as! [String: Any])
            self.button.isHidden = false
        }
    }

    //Funzione che permette di tornare alla pagina precedente nella UiWebView
    @objc func goBack() {
       let request = URLRequest(url: urlhomepage! as URL);
        myWebView.loadRequest(request)
        checkUrl(url: "https://web.parktogo.it")
    }

    //Funzione che si occupa della verifica degli url
    func ValidazioneURL(url: String) -> Bool {
        var check: Bool = true
        if urlPermessi.contains(url) {
            print("Url valido")
        } else {
            print("Url non valido")
            check = false
        }
        checkUrl(url: url)
        return check;
    }


    //Funzione eseguita all'avvio della UIWebView
    override func viewDidLoad() {

        super.viewDidLoad()
        self.view.removeAllConstraints()
        self.view.backgroundColor = UIColor(white: 1, alpha: 0.5)
        self.navigationController?.isNavigationBarHidden = true
        UIApplication.shared.keyWindow?.windowLevel = UIWindow.Level.statusBar

        //
        self.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        myWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        //Istanzio la UIWebView
        myWebView.delegate = self
        //Aggiuge la view all'UIView principale
        self.view.addSubview(myWebView)
        //Questo codice permette di modificare lo useragent per i response della UiWebView
        UserDefaults.standard.register(defaults: dictionatyUnclock as! [String: Any])
        //Visualizzo l'url all'interno della uiwebview
        let request = URLRequest(url: urlhomepage! as URL);
        myWebView.loadRequest(request);
        //Istanza del button che permette di tornare alla schermata precendente
        let image = UIImage(named: "arrow-back-icon.png")
        button.frame = CGRect(x: 0, y: 0, width: 80, height: 150)
        button.setImage(image, for: .normal)
        button.addTarget(self, action: #selector(goBack), for: .touchUpInside)
        //button.backgroundColor = .lightGray
        self.view.addSubview(button)
        checkUrl(url: "https://web.parktogo.it")


    }

//Funzione che viene eseguita ogni volta che verifica gli url presenti nell'array per verificarne la validità
    internal func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        var check: Bool = true
        do {
            //print("Url da verificare = \(request.url!.host!)")
            if navigationType == UIWebView.NavigationType.linkClicked {
                check = ValidazioneURL(url: request.url!.host!)
            }
        }
        catch is Error {

        }
        return check
    }

//Funzione eseguiata all'inzializzazione della webview
    func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebView.NavigationType) -> Bool {
        var check: Bool = true
        print("Url da verificare = \(request.url!.host!)")
        if navigationType == UIWebView.NavigationType.linkClicked {
            check = ValidazioneURL(url: request.url!.host!)
        }
        return check
    }


//Funzione eseguiata al caricamento della webview
    func webViewDidStartLoad(_ webView: UIWebView) {
        print("web view start loading")
    }
    override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(true)
        navigationController?.navigationBar.isHidden = true // for navigation bar hide
       // UIApplication.sharedApplication.statusBarHidden=true// for status bar hide
   }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
ios swift uiwebview fullscreen
1个回答
0
投票
似乎您将webview限制为safeAreaLayout,尝试将其限制为superView并查看其显示方式:)
© www.soinside.com 2019 - 2024. All rights reserved.