SYSearchBar is just an AirBnb-like search bar, which implements its own UI logic instead of using the components of UIKit
such as UISearchBar
/UISearchController
(iOS8+)/UISearchDisplayController
(<iOS8).
You can use SYSearchBar
via UIViewController
's category:
@interface UIViewController (SYSearchBar) <SYSearchButtonDelegate>
@property (nonatomic,readonly) CGFloat syInputBarTopInsets;
@property (nonatomic,retain,readonly) SYSearchButton *sySearchButton;
@property (nonatomic,retain,readonly) SYSearchInputBar *sySearchInputBar;
@property (nonatomic,retain) UIViewController *sySearchResultsViewController; // Setting your own view controller for results
- (void)syAddSearchBarInPosition:(CGPoint)pos;
- (void)syAddSearchBarInPosition:(CGPoint)pos topInsetsOfInputBar:(CGFloat)topInsets;
@end
Use the methods below to add a search bar in specified position to your view of the categoried view controller:
- (void)syAddSearchBarInPosition:(CGPoint)pos;
- (void)syAddSearchBarInPosition:(CGPoint)pos topInsetsOfInputBar:(CGFloat)topInsets;
The topInsets
specifies the top insets from the search bar to your view's top edge.
The properties syInputBarTopInsets
, sySearchButton
, sySearchInputBar
and sySearchResultsViewController
are explained as follows:
You should manually set search button's expanded
property according to the contentOffset
of your scroll view:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y > 20) {
self.sySearchButton.expanded = NO;
} else {
self.sySearchButton.expanded = YES;
}
}
The expanded search button (for a small contentOffset
):
Just implement the delegate methods of the inputTextField
property in the input bar
self.sySearchInputBar.inputTextField.delegate = self;
self.sySearchButton.placeholder = @"Search";
[self.sySearchInputBar.cancelButton setTitle:@"Close" forState:UIControlStateNormal];
The input bar's placeholder always keeps the same with the search button's.
SYSearchBar is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SYSearchBar'
Yu Xulu (Sean Yue), tonyfish@qq.com
SYSearchBar is available under the MIT license. See the LICENSE file for more info.