SFSafariViewController:隐藏导航栏

Mik*_*ell 5 objective-c ios swift sfsafariviewcontroller

我能够让我的应用程序通过SFSafariViewController按照这篇文章自动加载一个URL ,并且效果很好,唯一的缺点就是导航栏.

当以这种方式使用时,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.
    }
}
Run Code Online (Sandbox Code Playgroud)

-----工作.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
    }
}
Run Code Online (Sandbox Code Playgroud)

Sah*_*oor 6

把这段代码放进去 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
}
Run Code Online (Sandbox Code Playgroud)

要隐藏状态栏,View controller-based status bar appearance请YES在info.plist文件中设置并将其插入视图控制器中.

override func prefersStatusBarHidden() -> Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

警告:我建议您不要使用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)
}
Run Code Online (Sandbox Code Playgroud)

这种方法的问题只是覆盖在屏幕上,但如果你能找到一个漂亮的图像,你会没事的.

  • 多么可怕的黑客 (7认同)
  • "为了避免隐藏重新加载按钮,只需在SFSafariViewController中的done按钮上添加一个view/imageView,并且渲染按钮不可见或至少不可用." - 由于SafariViewController上的Apple文档,我不建议这样做.来自Apple文档:重要信息根据App Store Review Guidelines,此视图控制器必须用于向用户显示信息; 控制器可能不被其他视图或层隐藏或遮挡.此外,应用程序可能不会使用SFSafariViewController在未经用户知情和同意的情况下跟踪用户. (2认同)