How can i create a searchbar like safari
Just like the photos I posted That the searchbar becomes small or hidden when the screen is scrolled
...
It would be great if you could tell, what you've tried before. That way we can help you better.
In order to do that, you need a view of type UIScrollView
(UITableView
, UICollectionView
, etc.).
Implement this delegate method and it should do the trick.
Adjust the swipeThreshold
variable to fit your needs.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let swipeThreshold: CGFloat = 10
let y = scrollView.panGestureRecognizer.translation(in: scrollView).y
if y != 0 && swipeThreshold > y {
navigationController?.setNavigationBarHidden(true, animated: true)
} else {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
Old answer as reference:
Here is one way to do it — by using SFSafariViewController
using SafariServices
.
import UIKit
import SafariServices
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// Call this method when you want to show the browser
func doSomething() {
let url = URL(string: "https://www.google.com")!
let safariViewController = SFSafariViewController(url: url)
present(safariViewController, animated: true, completion: nil)
}
}