diff --git a/ModernSearchBar.podspec b/ModernSearchBar.podspec index ee2f7d4..2c58640 100755 --- a/ModernSearchBar.podspec +++ b/ModernSearchBar.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "ModernSearchBar" -s.version = "1.1" +s.version = "1.2" 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 d54789e..f093f49 100644 --- a/Pod/Classes/ModernSearchBar.swift +++ b/Pod/Classes/ModernSearchBar.swift @@ -48,6 +48,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS 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_searchIcon_height: CGFloat = 17 public var suggestionsView_searchIcon_width: CGFloat = 17 @@ -126,12 +127,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS // -------------------------------- public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { - if searchText.isEmpty { - self.closeSuggestionsView() - } else { - self.searchWhenUserTyping(caracters: searchText) - self.openSuggestionsView() - } + self.searchWhenUserTyping(caracters: searchText) self.delegateModernSearchBar?.searchBar?(searchBar, textDidChange: searchText) } @@ -256,7 +252,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS DispatchQueue.main.async { self.suggestionListFiltred.removeAll() self.suggestionListFiltred.append(contentsOf: suggestionListFiltredTmp) - self.updateAfterSearch() + self.updateAfterSearch(caracters: caracters) } } @@ -275,7 +271,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS DispatchQueue.main.async { self.suggestionListWithUrlFiltred.removeAll() self.suggestionListWithUrlFiltred.append(contentsOf: suggestionListFiltredWithUrlTmp) - self.updateAfterSearch() + self.updateAfterSearch(caracters: caracters) } } @@ -287,22 +283,34 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS return ((stringQueried.range(of: stringSearched, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)) != nil) } - private func updateAfterSearch(){ + private func updateAfterSearch(caracters: String){ self.suggestionsView.reloadData() self.updateSizeSuggestionsView() + caracters.isEmpty ? self.closeSuggestionsView() : self.openSuggestionsView() } // -------------------------------- // SUGGESTIONS VIEW UTILS // -------------------------------- + private func haveToOpenSuggestionView() -> Bool { + switch self.choice { + case .normal: + return !self.suggestionListFiltred.isEmpty + case .withUrl: + return !self.suggestionListWithUrlFiltred.isEmpty + } + } + private func openSuggestionsView(){ - if (!self.isSuggestionsViewOpened){ - self.animationOpening() - - self.addViewToParent(view: self.suggestionsShadow) - self.addViewToParent(view: self.suggestionsView) - self.isSuggestionsViewOpened = true + if (self.haveToOpenSuggestionView()){ + if (!self.isSuggestionsViewOpened){ + self.animationOpening() + + self.addViewToParent(view: self.suggestionsShadow) + self.addViewToParent(view: self.suggestionsView) + self.isSuggestionsViewOpened = true + } } } @@ -324,10 +332,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS UIView.animate(withDuration: 0.3, delay: 0.0, options: [], animations: { self.suggestionsView.alpha = 0.0 self.suggestionsShadow.alpha = 0.0 - }, completion: { (finished) -> Void in - self.suggestionsView.removeFromSuperview() - self.suggestionsShadow.removeFromSuperview() - }) + }, completion: nil) } // -------------------------------- @@ -335,11 +340,11 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS // -------------------------------- private func getSuggestionsViewX() -> CGFloat { - return self.getEditText().frame.origin.x + return self.getGlobalPointEditText().x } private func getSuggestionsViewY() -> CGFloat { - return self.frame.origin.y.adding(self.getEditText().frame.height).adding((self.frame.height.subtracting(self.getEditText().frame.height)).divided(by: 2)).subtracting(4) + return self.getShadowY().subtracting(self.suggestionsView_verticalSpaceWithSearchBar) } private func getSuggestionsViewWidth() -> CGFloat { @@ -351,11 +356,11 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS } private func getShadowX() -> CGFloat { - return self.frame.origin.x + return 0 } private func getShadowY() -> CGFloat { - return self.frame.origin.y.adding(self.frame.height) + return self.getGlobalPointEditText().y.adding(self.getEditText().frame.height) } private func getShadowHeight() -> CGFloat { @@ -363,7 +368,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS } private func getShadowWidth() -> CGFloat { - return self.frame.width + return (self.getTopViewController()?.view.frame.width)! } private func updateSizeSuggestionsView(){ @@ -415,6 +420,10 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS } } + private func getGlobalPointEditText() -> CGPoint { + return self.getEditText().superview!.convert(self.getEditText().frame.origin, to: nil) + } + private func interceptOrientationChange(){ self.getEditText().addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil) } diff --git a/README.md b/README.md index 682f665..ecd8300 100644 --- a/README.md +++ b/README.md @@ -116,11 +116,12 @@ 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 ``` ## Version -1.1 +1.2 ## License diff --git a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate index 866d6fe..95c7570 100644 Binary files a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.xcworkspace/xcuserdata/Philippe.xcuserdatad/UserInterfaceState.xcuserstate and b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample.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 bd22122..f464484 100644 --- a/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample/ViewController.swift +++ b/Sample/ModernSearchBar-Sample/ModernSearchBar-Sample/ViewController.swift @@ -149,6 +149,7 @@ 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 } private func makingSearchBarAwesome(){