我能够让我的应用程序根据这个 post 通过 SFSafariViewController 自动加载 url ,而且效果很好,唯一的缺点是导航栏。
SFSafariViewController 导航栏在以这种方式使用时有点无用,因为 url 是只读的,并且“完成”链接除了重新加载页面外什么都不做。因此,我想完全隐藏导航栏。
根据已接受答案所附的评论,建议将我的 Root View Controller 设置为 SFSafariViewController,但我无法正常工作。设置很简单,因为只有一个 View Controller ,其中包含上述帖子中的代码。
如何隐藏导航栏但仍保持 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
并将其插入您的 View Controller 。
override func prefersStatusBarHidden() -> Bool {
return true
}
警告:我建议您不要将 SFSafariViewController 用于全屏 View ,因为无法重新加载(因为重新加载按钮位于 UINavigationBar 中)。如果请求失败,它将使应用程序无用。 而是使用带有自定义工具栏的全屏 WKWebView。
更新: 为避免隐藏重新加载按钮,只需在 SFSafariViewController 中的完成按钮上添加一个 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)
}
这种方法的问题只是叠加层停留在屏幕上,但如果你能为它找到一个漂亮的图像,你会没事的。
关于ios - SFSafariViewController:隐藏导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813129/