SFSafariViewController

SFSafariViewController

本文介绍了SFSafariViewController:隐藏导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够让我的应用程序通过SFSafariViewController自动加载网址,效果很好,唯一的缺点是导航栏。

I was able to get my app to automatically load a url via the SFSafariViewController per this post, and it works great, the only drawback is the navbar.

当使用这种方式时,SFSafariViewController导航栏是没用的,因为url是只读的,而完成链接除了重新加载之外什么都不做页。因此,我想完全隐藏导航栏。

The SFSafariViewController navbar is kind of useless when being used this way, as the url is read-only, and the 'done' link doesn't do anything but reload the page. As such, I would like to hide the navbar altogether.

根据附加到已接受答案的注释,建议将我的根视图控制器设置为SFSafariViewController,我可以'开始工作设置很简单,因为有一个视图控制器,代码包含在前面提到的帖子中。

Per the comments attached to accepted answer, it was suggested to set my root view controller to SFSafariViewController which I can't get working. The setup is simple as there is a single view controller with the code included in the aforementioned post.

如何隐藏导航栏但仍然保留SFSafariViewController的好处?或者,如果我无法隐藏导航栏,至少隐藏完成链接?

How can I hide the navbar but still maintain the benefits of the SFSafariViewController? Or if I can't hide the navbar, at least hide the 'done' link?

代码段:

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.
    }
}

-----工程。 Navbar是隐藏的-----

----- Works. Navbar is "hidden" -----

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文件中查看基于控制器的状态栏外观 YES 并将其插入视图控制器中。

To hide the status bar, set View controller-based status bar appearance to YES in your info.plist file and insert this in your view controller.

override func prefersStatusBarHidden() -> Bool {
    return true
}

警告 :我建议你不要使用SFSafariViewController进行全屏视图,因为无法重新加载(因为重载按钮位于UINavigationBar中)。如果请求失败,它将使应用程序无效。
而不是使用自定义工具栏来全屏WKWebView。

Warning: I will suggest you against using SFSafariViewController for full screen view because reloading is not possible(since reload button is in UINavigationBar). In case request fails, it will render the application useless.Rather go for full screen WKWebView with custom toolbar instead.

更新
为避免隐藏重新加载按钮,只需在SFSafariViewController中的done按钮上添加一个view / imageView,并且渲染按钮不可见或至少不可用。

Update:To avoid hiding the reload button, just add a view/imageView over the done button in your SFSafariViewController and render button invisible or at least untappable.

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)
}

这种方法的问题是只有那个叠加层停留在屏幕上,但是如果你能找到一个漂亮的图像,你会没事的。

The problem with this approach is only that overlay stays on screen, but if you can find a nice image for it, you will be fine.

这篇关于SFSafariViewController:隐藏导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 15:42