diff --git a/Gifs/Gif_complex_list.gif b/Gifs/Gif_complex_list.gif index 8a08bd0..762a613 100644 Binary files a/Gifs/Gif_complex_list.gif and b/Gifs/Gif_complex_list.gif differ diff --git a/Gifs/Gif_simple_list.gif b/Gifs/Gif_simple_list.gif index 9676e2c..70bc7fb 100644 Binary files a/Gifs/Gif_simple_list.gif and b/Gifs/Gif_simple_list.gif differ diff --git a/ModernSearchBar.podspec b/ModernSearchBar.podspec index 2c58640..49d7432 100755 --- a/ModernSearchBar.podspec +++ b/ModernSearchBar.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "ModernSearchBar" -s.version = "1.2" +s.version = "1.3" s.summary = "ModernSearchBar" s.description = "The famous iOS search bar with auto completion feature implemented." s.homepage = "https://github.com/PhilippeBoisney/ModernSearchBar" diff --git a/Pod/Classes/ModernSearchBar.swift b/Pod/Classes/ModernSearchBar.swift index f093f49..78cb84b 100644 --- a/Pod/Classes/ModernSearchBar.swift +++ b/Pod/Classes/ModernSearchBar.swift @@ -27,6 +27,8 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS private var choice: Choice = .normal + private var keyboardHeight: CGFloat = 0 + //MARKS: VIEWS private var suggestionsView: UITableView! private var suggestionsShadow: UIView! @@ -43,17 +45,19 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS public var searchLabel_textColor: UIColor? public var searchLabel_backgroundColor: UIColor? - public var suggestionsView_maxHeight: CGFloat = UIScreen.main.bounds.height.divided(by: 2.5) + public var suggestionsView_maxHeight: CGFloat! public var suggestionsView_backgroundColor: UIColor? public var suggestionsView_contentViewColor: UIColor? public var suggestionsView_separatorStyle: UITableViewCellSeparatorStyle = .none public var suggestionsView_selectionStyle: UITableViewCellSelectionStyle = UITableViewCellSelectionStyle.none - public var suggestionsView_verticalSpaceWithSearchBar: CGFloat = 4 + public var suggestionsView_verticalSpaceWithSearchBar: CGFloat = 3 public var suggestionsView_searchIcon_height: CGFloat = 17 public var suggestionsView_searchIcon_width: CGFloat = 17 public var suggestionsView_searchIcon_isRound = true + public var suggestionsView_spaceWithKeyboard:CGFloat = 3 + //MARK: INITIALISERS required public init(coder aDecoder: NSCoder) { @@ -76,6 +80,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS self.delegate = self self.isSuggestionsViewOpened = false self.interceptOrientationChange() + self.interceptKeyboardChange() } private func configureViews(){ @@ -153,6 +158,22 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS self.endEditing(true) } + public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool { + if let shouldEndEditing = self.delegateModernSearchBar?.searchBarShouldEndEditing?(searchBar) { + return shouldEndEditing + } else { + return true + } + } + + public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { + if let shouldBeginEditing = self.delegateModernSearchBar?.searchBarShouldBeginEditing?(searchBar) { + return shouldBeginEditing + } else { + return true + } + } + // -------------------------------- // ACTIONS @@ -315,7 +336,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS } private func closeSuggestionsView(){ - if (self.isSuggestionsViewOpened){ + if (self.isSuggestionsViewOpened == true){ self.animationClosing() self.isSuggestionsViewOpened = false } @@ -373,22 +394,37 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS private func updateSizeSuggestionsView(){ var frame: CGRect = self.suggestionsView.frame - frame.size.height = self.getMaxHeightSuggestionsView(newHeight: self.suggestionsView.contentSize.height) - self.suggestionsView.frame = frame - UIView.animate(withDuration: 0.0) { + frame.size.height = self.getExactMaxHeightSuggestionsView(newHeight: self.suggestionsView.contentSize.height) + + UIView.animate(withDuration: 0.3) { + self.suggestionsView.frame = frame self.suggestionsView.layoutIfNeeded() self.suggestionsView.sizeToFit() } } - private func getMaxHeightSuggestionsView(newHeight: CGFloat) -> CGFloat { - if (newHeight > self.suggestionsView_maxHeight) { - return self.suggestionsView_maxHeight + private func getExactMaxHeightSuggestionsView(newHeight: CGFloat) -> CGFloat { + var estimatedMaxView: CGFloat! + if self.suggestionsView_maxHeight != nil { + estimatedMaxView = self.suggestionsView_maxHeight + } else { + estimatedMaxView = self.getEstimateHeightSuggestionsView() + } + + if (newHeight > estimatedMaxView) { + return estimatedMaxView } else { return newHeight } } + private func getEstimateHeightSuggestionsView() -> CGFloat { + return self.getViewTopController().frame.height + .subtracting(self.getShadowY()) + .subtracting(self.keyboardHeight) + .subtracting(self.suggestionsView_spaceWithKeyboard) + } + // -------------------------------- // UTILS // -------------------------------- @@ -400,6 +436,10 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS } } + private func getViewTopController() -> UIView{ + return self.getTopViewController()!.view + } + private func getTopViewController() -> UIViewController? { var topController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController while topController?.presentedViewController != nil { @@ -424,6 +464,10 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS return self.getEditText().superview!.convert(self.getEditText().frame.origin, to: nil) } + // -------------------------------- + // OBSERVERS CHANGES + // -------------------------------- + private func interceptOrientationChange(){ self.getEditText().addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil) } @@ -435,6 +479,25 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS } } + private func interceptKeyboardChange(){ + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) + } + + @objc private func keyboardWillShow(notification: NSNotification) { + let userInfo = notification.userInfo as! [String: NSObject] as NSDictionary + let keyboardFrame = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! CGRect + let keyboardHeight = keyboardFrame.height + + self.keyboardHeight = keyboardHeight + self.updateSizeSuggestionsView() + } + + @objc private func keyboardWillHide(notification: NSNotification) { + self.keyboardHeight = 0 + self.updateSizeSuggestionsView() + } + // -------------------------------- // PUBLIC ACCESS // -------------------------------- @@ -442,4 +505,5 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS public func getSuggestionsView() -> UITableView { return self.suggestionsView } + } diff --git a/Pod/Classes/ModernSearchBarCell.swift b/Pod/Classes/ModernSearchBarCell.swift index 06768e0..7b23758 100644 --- a/Pod/Classes/ModernSearchBarCell.swift +++ b/Pod/Classes/ModernSearchBarCell.swift @@ -32,7 +32,7 @@ public class ModernSearchBarCell: UITableViewCell { private func setup(){ ///Setup image self.imgModernSearchBar.translatesAutoresizingMaskIntoConstraints = false - self.imgModernSearchBar.contentMode = .scaleAspectFit + self.imgModernSearchBar.contentMode = .scaleAspectFill ///Setup label self.labelModelSearchBar.translatesAutoresizingMaskIntoConstraints = false diff --git a/README.md b/README.md index e2fc9b7..a194f0d 100644 --- a/README.md +++ b/README.md @@ -123,12 +123,13 @@ self.modernSearchBar.suggestionsView_backgroundColor = UIColor.brown self.modernSearchBar.suggestionsView_contentViewColor = UIColor.yellow self.modernSearchBar.suggestionsView_separatorStyle = .singleLine self.modernSearchBar.suggestionsView_selectionStyle = UITableViewCellSelectionStyle.gray -self.modernSearchBarsuggestionsView_verticalSpaceWithSearchBar = 10 +self.modernSearchBar.suggestionsView_verticalSpaceWithSearchBar = 10 +self.modernSearchBar.suggestionsView_spaceWithKeyboard = 20 ``` ## Version -1.2 +1.3 ## License diff --git a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcodeproj/project.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcodeproj/project.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate index 8f7e78f..5254899 100644 Binary files a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcodeproj/project.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate and b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcodeproj/project.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample/ViewController.swift b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample/ViewController.swift index f464484..78cb806 100644 --- a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample/ViewController.swift +++ b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample/ViewController.swift @@ -149,7 +149,8 @@ class ViewController: UIViewController, ModernSearchBarDelegate { self.modernSearchBar.suggestionsView_contentViewColor = UIColor.yellow self.modernSearchBar.suggestionsView_separatorStyle = .singleLine self.modernSearchBar.suggestionsView_selectionStyle = UITableViewCellSelectionStyle.gray - self.modernSearchBarsuggestionsView_verticalSpaceWithSearchBar = 10 + self.modernSearchBar.suggestionsView_verticalSpaceWithSearchBar = 10 + self.modernSearchBar.suggestionsView_spaceWithKeyboard = 20 } private func makingSearchBarAwesome(){