Closed Zach41 closed 7 years ago
I tried my library with UISearchController and it seems fine. I tested UISearchController with UITableViewController. If you have different configuration then please share demo.
Here is my Controller
static NSString * const ScoreCellIdentifier = @"ScoreCellIdentifier";
@interface ScoreController () <UISearchResultsUpdating, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UISearchController *searchController;
@end
@implementation ScoreController
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = ({
SearchResultController *resultController = [[SearchResultController alloc] init];
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:resultController];
searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
searchController.searchResultsUpdater = self;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor colorWithRed:0.918 green:0.933 blue:0.937 alpha:1.000]];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"取消"];
searchController.searchBar.placeholder = @"如“一年二班 数学 李狗蛋”";
searchController.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor]];
searchController.searchBar.delegate = self;
searchController;
});
self.tableView.tableHeaderView = _searchController.searchBar;
self.tableView.allowsSelection = NO;
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView registerClass:[ScoreControllerCell class] forCellReuseIdentifier:ScoreCellIdentifier];
self.title = @"成绩";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] bk_initWithImage:[UIImage imageNamed:@"naviPlusIcon"]
style:UIBarButtonItemStylePlain handler:^(id sender) {
// TODO: Navi Add Action
DDLogInfo(@"Navigation Add Button In ScoreController");
}];
}
#pragma mark - Table Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ScoreControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:ScoreCellIdentifier];
return cell;
}
#pragma mark - Table Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView fd_heightForCellWithIdentifier:ScoreCellIdentifier configuration:^(ScoreControllerCell *cell){
}];
}
#pragma mark - SearchController Updating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *text = searchController.searchBar.text;
DDLogInfo(@"Search Key: %@", text);
}
#pragma mark - SearchBar delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[_searchController dismissViewControllerAnimated:NO completion:^{
searchBar.showsCancelButton = NO;
}];
}
It's a UIViewController with a TableView as its property.
And this is my search result controller
static NSString * const SearchScoreCellID = @"SearchScoreCell";
@interface SearchResultController ()
@end
@implementation SearchResultController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[ScoreControllerCell class] forCellReuseIdentifier:SearchScoreCellID];
self.tableView.allowsSelection = NO;
self.tableView.tableFooterView = [UIView new];
}
#pragma mark - Table Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ScoreControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchScoreCellID];
return cell;
}
#pragma mark - Table Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView fd_heightForCellWithIdentifier:SearchScoreCellID configuration:^(ScoreControllerCell *cell) {
}];
}
It's just a UITableViewController.
I recorded the result, and there is gif.
There is my Podfile
source "https://git.oschina.net/6david9/Specs.git"
platform :ios, "8.0"
target 'SmartCamp' do
pod 'SVProgressHUD'
pod 'AFNetworking'
pod 'SDWebImage'
pod 'BlocksKit'
pod 'YYModel'
pod 'YYCache'
pod 'STPopup'
pod 'MJRefresh'
pod 'Masonry'
pod 'YYText'
pod 'CocoaLumberjack'
pod 'SDVersion'
pod 'UITableView+FDTemplateLayoutCell'
pod 'KMNavigationBarTransition'
pod 'MXParallaxHeader'
pod 'KVOController'
pod 'IQKeyboardManager'
end
When I removed IQKeyboardManager, search bar goes well, I really don't know why. :(
Thanks, I'll check.
I dont know how the cell will look because it doesn't contain ScoreControllerCell, ScoreControllerCell, fd_heightForCellWithIdentifier, bk_initWithImage. Can you wrap-up all this in a demo?
@Zach41 Do you have any updates?
I imported IQKeyboardManager in my project, and I used UISearchController in my project too, when I clicked cancel button in searchBar, searchBar shifted up weirdly. After I removed IQKeyboardManager, searchBar behaved correctly.