12

根据这篇文章,我能够让我的应用程序通过 SFSafariViewController 自动加载 url ,而且效果很好,唯一的缺点是导航栏。

SFSafariViewController 导航栏在以这种方式使用时有点无用,因为 url 是只读的,并且“完成”链接除了重新加载页面之外什么都不做。因此,我想完全隐藏导航栏。

根据附加到已接受答案的评论,建议将我的根视图控制器设置为我无法工作的 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
    }
}
4

4 回答 4

8

把这段代码放进去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

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

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

更新:为避免隐藏重新加载按钮,只需在 SFSafariViewController 中的完成按钮上添加一个视图/图像视图,然后使按钮不可见或至少不可点击。

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

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

于 2015-11-19T20:51:33.937 回答
2
import Foundation
import UIKit
import SafariServices

class MySafariFullScreenViewController: UIViewController  {

    override func viewDidLoad() {
        super.viewDidLoad()
        //WONT WORK read only you need to override it in this VC or in SFSafVC using extension - see bottom of this code
        //self.prefersStatusBarHidden = true

    }

    override func viewDidAppear(_ animated: Bool){
        let urlString = "https://......"
        //if a log screen - i think SFSafariViewController can handle this
        //let urlString = "https://<domain>login?redirect=https:<homescreen>"


        if let url: URL = URL(string: urlString) {
            let safariViewController = SFSafariViewController(url: url)
            present(safariViewController, animated: true) {

                var frame = safariViewController.view.frame
                //if status bar not hidden
                l//et OffsetY: CGFloat  = 64
                //if status bar hidden
                let OffsetY: CGFloat  = 44

                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


            }
        }else{
            //url error
        }



    }
    //this is for this vc - but for SFSafariVC you need override using extension
    override var prefersStatusBarHidden: Bool{
        get{
            return true
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension SFSafariViewController{
     override open var prefersStatusBarHidden: Bool{
        get{
            return true
        }
    }
}
于 2017-07-11T17:57:35.270 回答
2

自定义 SafariViewController 可能不是一个好主意。

苹果指南明确表示

必须使用 SafariViewController 以可视方式向用户呈现信息;控制器不能被其他视图或层隐藏或遮挡。此外,应用不得在未经用户知情和同意的情况下使用 SafariViewController 跟踪用户。

参考:- https://developer.apple.com/app-store/review/guidelines/

于 2019-08-07T17:22:04.040 回答
1

只需使用presentViewController:animated:completion:

https://stackoverflow.com/a/40460594/842655

于 2019-02-12T08:36:16.397 回答