Skip to content

Commit

Permalink
Merge pull request #12 from PhilippeBoisney/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
PhilippeBoisney committed Mar 14, 2017
2 parents c11cae1 + 864d1b8 commit e76ed97
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 14 deletions.
Binary file modified Gifs/Gif_complex_list.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Gifs/Gif_simple_list.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 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"
Expand Down
82 changes: 73 additions & 9 deletions Pod/Classes/ModernSearchBar.swift
Expand Up @@ -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!
Expand All @@ -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) {
Expand All @@ -76,6 +80,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
self.delegate = self
self.isSuggestionsViewOpened = false
self.interceptOrientationChange()
self.interceptKeyboardChange()
}

private func configureViews(){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
// --------------------------------
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -435,11 +479,31 @@ 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
// --------------------------------

public func getSuggestionsView() -> UITableView {
return self.suggestionsView
}

}
2 changes: 1 addition & 1 deletion Pod/Classes/ModernSearchBarCell.swift
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -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

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

0 comments on commit e76ed97

Please sign in to comment.