SFSafariViewController:隐藏导航栏
我能够让我的应用程序通过SFSafariViewController根据这个post自动加载一个url,它工作的很好,唯一的缺点是导航栏。SFSafariViewController:隐藏导航栏
当以这种方式使用SFSafariViewController导航栏时,这种方式毫无用处,因为url是只读的,'done'链接除了重新加载页面外没有其他任何操作。因此,我想完全隐藏导航栏。
根据接受的答案附带的评论,有人建议将我的根视图控制器设置为SFSafariViewController,我无法工作。设置很简单,因为有一个视图控制器,其代码包含在上述文章中。
如何隐藏导航栏,但仍然保持SFSafariViewController的好处?或者,如果我无法隐藏导航栏,至少隐藏“完成”链接?
代码片段:
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.
}
}
-----作品。导航栏“隐藏” -----
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 appearance
到YES
并在此插入您的视图控制器。
override func prefersStatusBarHidden() -> Bool {
return true
}
警告:我会建议你反对,因为重装是不可能的(因为Reload按钮是在UINavigationBar的)使用SFSafariViewController全屏视图。如果请求失败,它将使应用程序无效。 改为使用自定义工具栏进行全屏WKWebView。
更新: 为了避免隐藏Reload按钮,只需在您的SFSafariViewController添加视图/ ImageView的在完成按钮和渲染按钮无形的或至少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)
}
这种方法的问题仅仅是覆盖在画面上停留,但如果你能找到它一个很好的形象,你将被罚款。
Ic,所以建议将svc对象向上移动64个像素,从而“隐藏”导航栏。有用。剩下的唯一问题是'prefersStatusBarHidden'重写似乎不起作用,因为状态栏仍在呈现。 –
我更新了答案。 –
有'prefersStatusBarHidden'工作。谢谢。但是,SFSafariViewController不能提供更多的优势,如cookie处理和更好的JavaScript渲染(通过硝基)?我们现在可以通过手机支持FB股票,因此cookie处理对我们来说是巨大的。 –
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
}
}
}
你可以继承'SFSafariViewController'并将导航栏隐藏在'viewWillAppear:'? – JAL
我可以尝试......我是helio newb在ios开发,所以任何代码片段都会帮助。 –
你可以尝试像这样:'self.navigationItem.rightBarButtonItem = nil' –