UiWebView 全屏显示

Posted

技术标签:

【中文标题】UiWebView 全屏显示【英文标题】:UiWebView display in fullscreen 【发布时间】:2020-04-03 09:11:35 【问题描述】:

下面的代码执行一个 UiViewController 的显示,其中包含一个 UiWebView,UiWe​​bView 必须全屏显示,而它显示如下图,我尝试了所有我知道的方法,通过修改故事板,通过 swift 编程,在下面你可以找到视图代码和源代码库,你能解释一下如何把这个 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()
    


【问题讨论】:

【参考方案1】:

您的UIWebView 已被限制为superView,但myWebViewUIWebBrowserView 显示在safeArea 下。 顺便说一句,UIWebView 已被弃用,请查看下面的链接。

只需将UIWebView 更改为WKWebView

导入WebKit
import WebKit

初始化

这些来自 OP 的 github 存储库的意大利 cmets 所以故意将它们添加到这里

//Definizione della UIWebView
var myWebView = WKWebView()

loadRequest 行更改为
myWebView.load(request)

viewDidLoad
myWebView = WKWebView(frame: CGRect(x: 0, y: 0,
                                            width: view.frame.width,
                                            height: view.frame.height))

然后跑!


奖金

UIWebView 已弃用 -> Apple Docs

WKWebView 文档 -> Apple Docs

WKWebView 用法->Hackingwithswift

【讨论】:

【参考方案2】:

您似乎将 webview 限制为 safeAreaLayout 尝试将其限制为 superView 并看看它会如何显示:)

【讨论】:

@如何改为约束到 superView? See the size inspector on the storyboard for the component 点击其中一个保险柜对齐一个然后change the first item to SuperView @riki 抱歉回复晚了:(

以上是关于UiWebView 全屏显示的主要内容,如果未能解决你的问题,请参考以下文章

如何强制在 UIWebView 中播放的视频在 iPad 上全屏显示?

使用UiWebview显示全屏Youtube视频

双击 UIwebview 到全屏 [关闭]

全屏UIWebView隐藏状态栏

在iOS 8中如何在UIWebView Player中接收全屏模式的通知?

带有分页的全屏 UICollectionView 内的 UIWebView