SFSafariViewController:隐藏导航栏

我可以使我的应用程序通过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

}

要隐藏状态栏,View controller-based status bar

appearanceYES在info.plist文件中将其设置为并将其插入到视图控制器中。

override func prefersStatusBarHidden() -> Bool {

return true

}

:我建议您不要使用SFSafariViewController进行全屏查看,因为无法重新加载(因为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)

}

这种方法的问题仅在于覆盖层保留在屏幕上,但是如果您能找到一个不错的图像,那就可以了。

以上是 SFSafariViewController:隐藏导航栏 的全部内容, 来源链接: utcz.com/qa/429110.html

回到顶部