根据这篇文章,我能够让我的应用程序通过 SFSafariViewController 自动加载 url ,而且效果很好,唯一的缺点是导航栏。
SFSafariViewController 导航栏在以这种方式使用时有点无用,因为 url 是只读的,并且“完成”链接除了重新加载页面之外什么都不做。因此,我想完全隐藏导航栏。
根据附加到已接受答案的评论,建议将我的根视图控制器设置为我无法工作的 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
}
}