Forked from wanbok/UISearchControllerDelegateExtension.swift
Created
July 5, 2017 22:21
-
-
Save drinkius/b34af5852b3bd0dc1e9af00acdc25dcc to your computer and use it in GitHub Desktop.
UISearchControllerDelegate
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
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