Created
April 25, 2016 06:46
-
-
Save wanbok/a6591a87fef2ad10249993fd90fe9cd3 to your computer and use it in GitHub Desktop.
UISearchControllerDelegate
This file contains hidden or 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
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