SFSafariViewController: Hide navigation bar

倾然丶 夕夏残阳落幕 提交于 2019-11-29 07:43:27
Sahil Kapoor

Put this code in viewDidAppear:

let safariViewController = SFSafariViewController(URL: url)
presentViewController(safariViewController, animated: true) {
    var frame = safariViewController.view.frame
    let OffsetY: CGFloat  = 64
    frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
    frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
    safariViewController.view.frame = frame
}

To hide the status bar, set View controller-based status bar appearance to YES in your info.plist file and insert this in your view controller.

override func prefersStatusBarHidden() -> Bool {
    return true
}

Warning: I will suggest you against using SFSafariViewController for full screen view because reloading is not possible(since reload button is in UINavigationBar). In case request fails, it will render the application useless. Rather go for full screen WKWebView with custom toolbar instead.

Update: To avoid hiding the reload button, just add a view/imageView over the done button in your SFSafariViewController and render button invisible or at least untappable.

presentViewController(svc, animated: true) {
    let width: CGFloat = 66
    let x: CGFloat = self.view.frame.width - width

    // It can be any overlay. May be your logo image here inside an imageView.
    let overlay = UIView(frame: CGRect(x: x, y: 20, width: width, height: 44))
    overlay.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
    svc.view.addSubview(overlay)
}

The problem with this approach is only that overlay stays on screen, but if you can find a nice image for it, you will be fine.

Just present it using presentViewController:animated:completion:

https://stackoverflow.com/a/40460594/842655

Customising SafariViewController may not be a good idea.

The Apple Guidelines clearly says

SafariViewController must be used to visibly present information to users; the controller may not be hidden or obscured by other views or layers. Additionally, an app may not use SafariViewController to track users without their knowledge and consent.

Refer:-https://developer.apple.com/app-store/review/guidelines/

import Foundation
import UIKit
import SafariServices

class MySafariFullScreenViewController: UIViewController  {

    override func viewDidLoad() {
        super.viewDidLoad()
        //WONT WORK read only you need to override it in this VC or in SFSafVC using extension - see bottom of this code
        //self.prefersStatusBarHidden = true

    }

    override func viewDidAppear(_ animated: Bool){
        let urlString = "https://......"
        //if a log screen - i think SFSafariViewController can handle this
        //let urlString = "https://<domain>login?redirect=https:<homescreen>"


        if let url: URL = URL(string: urlString) {
            let safariViewController = SFSafariViewController(url: url)
            present(safariViewController, animated: true) {

                var frame = safariViewController.view.frame
                //if status bar not hidden
                l//et OffsetY: CGFloat  = 64
                //if status bar hidden
                let OffsetY: CGFloat  = 44

                frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
                frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
                safariViewController.view.frame = frame


            }
        }else{
            //url error
        }



    }
    //this is for this vc - but for SFSafariVC you need override using extension
    override var prefersStatusBarHidden: Bool{
        get{
            return true
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension SFSafariViewController{
     override open var prefersStatusBarHidden: Bool{
        get{
            return true
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!