14

I'm using SafariViewControllerto display a webpage, and rather than the default "done" button I push from my app's NavigationController to preserve my nav stack and back arrow. However, I need to hide the default Done button and search bar on the SafariViewController. Is that possible yet? See my code and screen shot below...

let svc = SFSafariViewController(URL: pinterestSafariURL)
            navigationController?.pushViewController(svc, animated: true)

enter image description here

note: linking to this question, but the answer is a hack whereas I was looking for a solution using SafariViewController API : SFSafariViewController: Hide navigation bar

Community
  • 1
  • 1
GarySabo
  • 5,806
  • 5
  • 49
  • 124

2 Answers2

11

Per Apple's documentation on SFSafariViewController, there does not appear to be a publicly-accessible way to hide either the Done button or the URL bar. Apple suggests that you use WKWebView if you need a custom browser interface.

There's a AppCoda tutorial on WKWebView that shows you how to create a ViewController with an embedded WKWebView. Hope that helps!

BAP
  • 1,292
  • 12
  • 10
1

Brace yourself for ugliness and brittleness:

extension MyViewController: SFSafariViewControllerDelegate
{
    func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool)
    {
        let f = CGRect(origin: CGPoint(x: 0, y: 20),
                       size: CGSize(width: 90, height: 44))
        let uglyView = UIView(frame: f)
        uglyView.backgroundColor = svc.view.backgroundColor
        controller.view.addSubview(uglyView)
    }
}

obviously origin y needs fixin' for iPhone X for this to work more or less.

UPD: Sharon implies down below (in the comments) that such a hackery is likely to cause the app rejected by apple.

I'd love to see Apple fix this with .none done button styling available in iOS 14.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • 2
    according to the docs: In accordance with App Store Review Guidelines, this view controller must be used to visibly present information to users; the controller may not be hidden or obscured by other views or layers. Additionally, an app may not use SFSafariViewController to track users without their knowledge and consent. – sharon Jan 07 '20 at 11:55
  • 3
    So you could have made an answer of your own. A compact one saying: "You can not" – Anton Tropashko Jan 13 '20 at 11:33