I write UISearchBar in my TopBar.m like this:
_tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)];
_tempSearchBar.barStyle=UIBarStyleDefault;
_tempSearchBar.placeholder=@"搜索";
[self addSubview:_tempSearchBar];
结果如下,是正确的。
然后我在另一个类中这样写UISearchDisplayController:
_topBar.tempSearchBar.delegate = self;
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_topBar.tempSearchBar contentsController:self];
[_searchDisplayController setDelegate:self];
[_searchDisplayController setSearchResultsDataSource:self];
UISearchBarDelegate像这样:
#pragma mark -
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchDisplayController setActive:YES animated:YES];
}
当我点击UISearchBar时,它会显示如下,searchBar的框架被改变了。为什么?
![enter image description here](https://istack.dev59.com/aHwTt.webp)
![enter image description here](https://istack.dev59.com/ByFai.webp)
谢谢。
UIView *searchContainerView = [UIView new]; [searchContainerView addSubview:self.searchController.searchBar]; UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchContainerView]; self.navigationItem.rightBarButtonItem = searchBarItem; CGRect bounds = self.navigationController.view.frame; bounds= CGRectMake(120, 0, bounds.size.width-120, 44); [searchContainerView setFrame:bounds];
- drpawelo