Skip to content

Commit

Permalink
Merge pull request #10 from PhilippeBoisney/dev
Browse files Browse the repository at this point in the history
Version 1.2
  • Loading branch information
PhilippeBoisney authored Mar 7, 2017
2 parents bb8323b + 507ae36 commit c77d1c4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ModernSearchBar.podspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
57 changes: 33 additions & 24 deletions Pod/Classes/ModernSearchBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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
}
}
}

Expand All @@ -324,22 +332,19 @@ 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)
}

// --------------------------------
// VIEW UTILS
// --------------------------------

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 {
Expand All @@ -351,19 +356,19 @@ 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 {
return (self.getTopViewController()?.view.frame.height)!
}

private func getShadowWidth() -> CGFloat {
return self.frame.width
return (self.getTopViewController()?.view.frame.width)!
}

private func updateSizeSuggestionsView(){
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit c77d1c4

Please sign in to comment.