How to push SFSafariViewController?

末鹿安然 提交于 2019-12-09 05:10:13

问题


2 strange things happen when I try to push Safari ViewController:

  1. Its adress bar with Done button is placed below my Navigation Bar;

  2. Delegate method safariViewControllerDidFinish: does not get called when I press back button.

I don't think Apple would approve of this behavoir, so:

Is there a way to push Safari ViewController without these problems?


回答1:


Do not push a SFSafariViewController with the pushViewController:animated: method, instead, use the presentViewController:animated:completion: method.

The Safari view controller will be presented with a standard push animation.




回答2:


In addition to rckoenes comment the only other option i see is to hide the navigation bar when presenting an SFSafariViewController

import UIKit
import SafariServices

class ViewController: UIViewController {
    @IBAction func openBrowser(sender: AnyObject) {
        let safariViewController = SFSafariViewController(URL: NSURL(string: "http://your.url")!)
        safariViewController.delegate = self

        // hide navigation bar and present safari view controller
        navigationController?.navigationBarHidden = true
        navigationController?.pushViewController(safariViewController, animated: true)
    }
}

extension ViewController: SFSafariViewControllerDelegate {
    func safariViewControllerDidFinish(controller: SFSafariViewController) {
        // pop safari view controller and display navigation bar again
        navigationController?.popViewControllerAnimated(true)
        navigationController?.navigationBarHidden = false
    }
}


来源:https://stackoverflow.com/questions/32604411/how-to-push-sfsafariviewcontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!