clo*_*sen 3 uisearchbar uisearchdisplaycontroller uisearchbardelegate uisearchbardisplaycontrol ios
我在我的TopBar.m中写UISearchBar是这样的:
_tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)];
_tempSearchBar.barStyle=UIBarStyleDefault;
_tempSearchBar.placeholder=@"??";
[self addSubview:_tempSearchBar];
Run Code Online (Sandbox Code Playgroud)
结果是这样,是对的.

然后我在另一个类中编写UISearchDisplayController,如下所示:
_topBar.tempSearchBar.delegate = self;
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_topBar.tempSearchBar contentsController:self];
[_searchDisplayController setDelegate:self];
[_searchDisplayController setSearchResultsDataSource:self];
Run Code Online (Sandbox Code Playgroud)
UISearchBarDelegate是这样的:
#pragma mark -
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchDisplayController setActive:YES animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
当我单击UISearchBar时,它显示如下,searchBar的框架被更改.为什么?

当我取消UISearchDisplayController时,它是这样的:

为什么框架改变了?UISearchDisplayController的宽度从320-44变为320?
谢谢.
searchBar的框架由UIKit更改,因此我自己更改了searchBar的框架.
我在下面的委托中更改了searchBar的框架.
一个是UISearchBar的代表:
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
}
Run Code Online (Sandbox Code Playgroud)
另一个是UISearchDisplayController的委托:
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
[controller.searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
[self.searchDisplayController.searchResultsTableView setDelegate:self];
}
Run Code Online (Sandbox Code Playgroud)
它可以工作,我可以得到正确的框架,但当我点击searchBar它会摇一点.
这不是最好的方法,但它可以工作.有没有人有更好的方法?
更新:我已经调试了UISearchBar和UISearchDisplayController几个小时,但它有一个小错误:当我endEditing时,searchBar的宽度将变为320px,然后将成为我的宽度.我无法更改cancelButton的背景颜色.所以我写了一个自定义的SearchDisplayController,它带有一个UISearchBar属性和一个UITableView属性.这对我来说很有用.
| 归档时间: |
|
| 查看次数: |
6737 次 |
| 最近记录: |