Skip to content

Instantly share code, notes, and snippets.

@wanbok
Created April 25, 2016 06:46
Show Gist options
  • Save wanbok/a6591a87fef2ad10249993fd90fe9cd3 to your computer and use it in GitHub Desktop.
Save wanbok/a6591a87fef2ad10249993fd90fe9cd3 to your computer and use it in GitHub Desktop.
UISearchControllerDelegate
extension ViewController: UISearchControllerDelegate {
func willPresentSearchController(searchController: UISearchController) {
tabBarController?.tabBar.hidden = true
searchBarBackground(true)
dimmingBackground(true)
}
func willDismissSearchController(searchController: UISearchController) {
tabBarController?.tabBar.hidden = false
searchBarBackground(false)
dimmingBackground(false)
}
// For animate
func searchBarBackground(on: Bool) {
let onAlpha: CGFloat = 0.95
let offAlpha: CGFloat = 0
let barBackgroundView = searchController.view?.subviews
.filter { $0.frame.height < 100 }.first
barBackgroundView?.alpha = on ? offAlpha : onAlpha
UIView.animateWithDuration(0.3) { barBackgroundView?.alpha = on ? onAlpha : offAlpha }
}
func dimmingBackground(on: Bool) {
let onColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
let offColor = UIColor.blackColor().colorWithAlphaComponent(0)
let backgroundView = searchController.view?.subviews
.filter { $0.frame.height == UIScreen.mainScreen().bounds.height }.first
backgroundView?.backgroundColor = on ? offColor : onColor
UIView.animateWithDuration(0.3) {
backgroundView?.backgroundColor = on ? onColor : offColor
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment