我能够让我的应用程序按照post通过SFSafariViewController自动加载网址,并且效果很好,唯一的缺点是导航栏。
当以这种方式使用时,SFSafariViewController导航栏是没用的,因为url是只读的,并且“完成”#33;链接没有做任何事情,但重新加载页面。因此,我想完全隐藏导航栏。
根据接受回答的评论,建议将我的根视图控制器设置为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.
}
}
-----工作。 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
}
}
答案 0 :(得分:5)
将此代码放入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
}
警告:我建议您不要使用SFSafariViewController进行全屏显示,因为无法重新加载(因为重新加载按钮位于UINavigationBar中)。如果请求失败,它将使应用程序无效。 而是选择使用自定义工具栏的全屏WKWebView。
<强>更新强>: 要避免隐藏重新加载按钮,只需在SFSafariViewController中的done按钮上添加一个view / imageView,并且渲染按钮不可见或至少不可用。
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)
}
这种方法的问题只是覆盖在屏幕上,但如果你能找到一个漂亮的图像,你会没事的。
答案 1 :(得分:1)
只需使用presentViewController:animated:completion:
展示它
答案 2 :(得分:1)
自定义SafariViewController可能不是一个好主意。
Apple指南明确规定
必须使用SafariViewController向用户显示信息。控制器可能不会被其他视图或图层隐藏或遮盖。此外,在未经用户了解和同意的情况下,应用程序可能无法使用SafariViewController来跟踪用户。
引用:-{https://developer.apple.com/app-store/review/guidelines/
答案 3 :(得分:0)
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
}
}
}