在项目中突然想把以前的搜索改成UISearchController,随便写个demo用着挺爽,没想到用的项目中会这么多坑,还好很多坑网上都能找到,但是这一个可是给我折腾死了,现在虽然是解决了,还是没找到原因,只能先在这记一下。有哪位发现原因了可要发我一下哟!!!
- 首先懒加载
#pragma mark - getter
- (UITableView *)tableView {
if (!_tableView) {
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundColor = [UIColor clearColor];
_tableView = tableView;
}
return _tableView;
}
- (UISearchController *)searchController {
if (!_searchController) {
MRCOANewlySearchViewController *searchVC = [[MRCOANewlySearchViewController alloc] init];
searchVC.newlyViewModel = self.viewModel;
//初始化
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchVC];
// searchController.delegate = self;
searchController.searchResultsUpdater = searchVC;
searchController.dimsBackgroundDuringPresentation = NO;
UISearchBar *searchBar = searchController.searchBar;
searchBar.placeholder = @"搜索";
searchBar.searchBarStyle = UISearchBarStyleMinimal;
[searchBar setBackgroundImage:[UIImage new]];
[searchBar setBackgroundColor:[UIColor clearColor]];
UIImage *image = [[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(SCREEN_WIDTH, 34)] imageByRoundCornerRadius:5];
[searchBar setSearchFieldBackgroundImage:image forState:UIControlStateNormal];
// searchBar.searchTextPositionAdjustment = UIOffsetMake(3, 0);
// CGFloat offsetX = (SCREEN_WIDTH-100) / 2;
// [searchBar setPositionAdjustment:UIOffsetMake(offsetX, 0) forSearchBarIcon:UISearchBarIconSearch];
[searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
//UIViewController中的属性,将UISearchViewController的view添加在了当前控制器View上。避免searchBar被遮挡,或者cell跳转时候依然在上面的情况
self.definesPresentationContext = YES;
_searchController = searchController;
}
return _searchController;
}
- 然后加载到view
#pragma mark - UI
- (void)addSubviews {
self.view.backgroundColor = [UIColor colorWithHexString:@"#ededed"];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"officialAccount_list_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(pushOfficicaAccountList)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.tableView setContentOffset:CGPointMake(0, CGRectGetHeight(self.searchController.searchBar.frame)) animated:NO];
MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMessage)];
footer.onlyRefreshPerDrag = YES;
footer.stateLabel.text = @"正在加载";
self.tableView.mj_footer = footer;
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}
我说一下我这大坑
Untitled.giftableView直接出现了这一片空白,我试图找到规律,在5s手机上测试
屏幕高度568 view高度504 行高定死100
一行 contentSize.height = 616;
两行 contentSize.height = 672;
三行 contentSize.height = 728;
四行 contentSize.height = 784;
...
十行 contentSize.height = 76;
616-100=516
672-200=472
728-300=428
784-400=384
516-472=44
472-428=44
428-384=44
516=44*10+76
到这我也就没再去分析(项目太紧后续也没跟进)
然后我突然想到将searchBar加到一个view上,之后在添加到tableHeaderView,没想到问题竟然解决了
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.searchController.searchBar.height)];
[headerView addSubview:self.searchController.searchBar];
self.tableView.tableHeaderView = headerView;
这就是我遇到的奇葩问题,仅记录下有望参考
网友评论