-
-
Save demalex/e81b4e7ccfb95b8ae680aee3e2d1fca5 to your computer and use it in GitHub Desktop.
Transition to UISearchController's search bar from UITextField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIView { | |
func makeConstraintsEqual(to view: UIView) { | |
if view.translatesAutoresizingMaskIntoConstraints == true { | |
view.translatesAutoresizingMaskIntoConstraints = false | |
} | |
NSLayoutConstraint.activate([ | |
self.topAnchor.constraint(equalTo: view.topAnchor), | |
self.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
self.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
self.trailingAnchor.constraint(equalTo: view.trailingAnchor) | |
]) | |
} | |
} | |
class PresentationViewController: ViewController<PresentationViewModel>, UISearchBarDelegate, UISearchControllerDelegate, UITextFieldDelegate { | |
var searchController: UISearchController! | |
var containerView: UIView! | |
var textField: UITextField! | |
var topConstraint: NSLayoutConstraint! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.title = "Search controller" | |
self.view.backgroundColor = .white | |
self.automaticallyAdjustsScrollViewInsets = false | |
self.containerView = UIView(frame: CGRect.zero) | |
self.containerView.translatesAutoresizingMaskIntoConstraints = false | |
self.view.addSubview(self.containerView) | |
if #available(iOS 11.0, *) { | |
self.topConstraint = self.containerView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor) | |
NSLayoutConstraint.activate([ | |
self.containerView.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor), | |
self.topConstraint, | |
self.containerView.widthAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.widthAnchor), | |
self.containerView.heightAnchor.constraint(equalToConstant: 56) | |
]) | |
} else { | |
self.topConstraint = self.containerView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor) | |
NSLayoutConstraint.activate([ | |
self.containerView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), | |
self.topConstraint, | |
self.containerView.widthAnchor.constraint(equalTo: self.view.widthAnchor), | |
self.containerView.heightAnchor.constraint(equalToConstant: 56) | |
]) | |
} | |
self.searchController = UISearchController(searchResultsController: nil) | |
self.searchController.delegate = self | |
self.definesPresentationContext = true | |
self.searchController.searchBar.alpha = 0 | |
self.containerView.addSubview(self.searchController.searchBar) | |
self.textField = UITextField(frame: CGRect.zero) | |
self.textField.borderStyle = .roundedRect | |
self.textField.delegate = self | |
self.textField.translatesAutoresizingMaskIntoConstraints = false | |
self.textField.placeholder = "Search" | |
self.containerView.addSubview(self.textField) | |
self.textField.makeConstraintsEqual(to: self.containerView) | |
} | |
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { | |
self.view.removeConstraint(self.topConstraint) | |
if #available(iOS 11.0, *) { | |
self.topConstraint = self.containerView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor) | |
} else { | |
self.topConstraint = self.containerView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor) | |
} | |
self.topConstraint.isActive = true | |
self.searchController.searchBar.alpha = 0 | |
UIView.animate(withDuration: 0.3, animations: { | |
self.view.layoutIfNeeded() | |
self.textField.alpha = 0 | |
self.searchController.searchBar.alpha = 1 | |
}) { (_) in | |
self.searchController.isActive = true | |
} | |
return false | |
} | |
func didPresentSearchController(_ searchController: UISearchController) { | |
searchController.searchBar.becomeFirstResponder() | |
} | |
func didDismissSearchController(_ searchController: UISearchController) { | |
self.view.removeConstraint(self.topConstraint) | |
self.topConstraint = self.containerView.topAnchor.constraint(equalTo: self.view.centerYAnchor) | |
self.topConstraint.isActive = true | |
self.textField.alpha = 0 | |
UIView.animate(withDuration: 0.3, animations: { | |
self.view.layoutIfNeeded() | |
self.textField.alpha = 1 | |
self.searchController.searchBar.alpha = 0 | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment