SFSafariViewController:隐藏导航栏

我能够让我的应用程序通过SFSafariViewController自动加载一个url,这个post ,它工作的很好,唯一的缺点是导航栏。

SFSafariViewController导航栏在使用这种方式时是没用的,因为url是只读的,'done'链接除了重新加载页面之外什么都不做。 因此,我想完全隐藏导航栏。

根据接受的答案附带的意见,build议将我的根视图控制器设置为SFSafariViewController,我不能工作。 该设置很简单,因为有一个单一的视图控制器与上述职位中包含的代码。

我怎样才能隐藏导航栏,但仍然保持SFSafariViewController的好处? 或者,如果我不能隐藏导航栏,至less隐藏“完成”的链接?

代码片段:

import UIKit import SafariServices class ViewController: UIViewController { private var urlString:String = "https://example.com" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!) self.presentViewController(svc, animated: true, completion: nil) self.navigationItem.rightBarButtonItem = nil } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

—–作品。 Navbar是“隐藏的”—–

 import UIKit import SafariServices class ViewController: UIViewController { private var urlString:String = "https://example.com" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // This will remove the status (battery, time, etc) bar UIApplication.sharedApplication().statusBarHidden = true } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!) // Kind of a hack, in that we really aren't removing the navbar // Rather we are adjusting the starting point of the vpc object so it appears as the navbar is hidden self.presentViewController(svc, animated: true) { var frame = svc.view.frame let OffsetY: CGFloat = 42 frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY) frame.size = CGSize(width: frame.size.width, height: frame.size.height + OffsetY) svc.view.frame = frame } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // For this to work be sure to set the following setting to OFF, in info.plist // 'View controller-based status bar appearance' override func prefersStatusBarHidden() -> Bool { return true } } 

把这个代码放在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 } 

要隐藏状态栏,请在您的info.plist文件中将View controller-based status bar appearanceYES ,并将其插入视图控制器中。

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

警告 :我build议你不要使用SFSafariViewController全屏视图,因为重装是不可能的(因为重载button在UINavigationBar中)。 在请求失败的情况下,它将使应用程序无用。 而是去自定义工具栏的全屏WKWebView。

更新 :为了避免隐藏重载button,只需在SFSafariViewController中的donebutton上添加view / imageView,然后渲染button不可见或至less不可用。

 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) } 

这种方法的问题只是覆盖屏幕上,但如果你能find一个好的图像,你会没事的。

 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 } } }