SFSafariViewController: hide navigation bar

I was able to get my application to automatically load the URL through the SFSafariViewController to this post , and it works fine, the only drawback is navbar.

The SFSafariViewController navigation bar is useless when using this method because the URL is read-only and the done link does nothing but reload the page. Thus, I would like to completely hide the navigation.

In the comments on the accepted answer, it was suggested to install my root view controller in the SFSafariViewController, which I cannot get. The setup is simple as there is one view controller with the code included in the above post.

How to hide the navigation bar, but at the same time retain the benefits of SFSafariViewController? Or, if I can’t hide the navigation bar, at least hide the "done" link?

Code snippet:

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

----- Works. Navbar "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 } } 
+5
source share
4 answers

Put this code in 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 } 

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

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

Warning I suggest you use the SFSafariViewController to view in full screen mode, since a reboot is not possible (since the reload button is in the UINavigationBar). If the request fails, the application will be useless. Instead, go to full-screen WKWebView with a customizable toolbar.

Update : To avoid hiding the reset button, simply add a view / imageView above the "done" button in your SFSafariViewController and a render button that is invisible or at least not recyclable.

 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 the overlay remains on the screen, but if you can find a nice image for it, you'll be fine.

+5
source

Just imagine this using presentViewController:animated:completion:

fooobar.com/questions/454649 / ...

+1
source

Setting up a SafariViewController may not be a good idea.

The Apple Guide clearly states

SafariViewController should be used to visually present information to users; the controller cannot be hidden or hidden by other views or layers. In addition, the application cannot use the SafariViewController to track users without their knowledge and consent.

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

+1
source
 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 } } } 
0
source

Source: https://habr.com/ru/post/1247092/


All Articles