我能夠讓我的應用程序通過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
}
}
你可以繼承'SFSafariViewController'並將導航欄隱藏在'viewWillAppear:'? – JAL
我可以嘗試......我是helio newb在ios開發,所以任何代碼片段都會幫助。 –
你可以嘗試像這樣:'self.navigationItem.rightBarButtonItem = nil' –