为什么我的UISearchBar的框架会被UISearchDisplayController改变?

3

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 当我取消UISearchDisplayController时,它是这样的: enter image description here 为什么框架会改变?UISearchDisplayController将宽度从320-44更改为320吗?
谢谢。
4个回答

12

UISearchDisplayController会将搜索栏扩展到其父视图的宽度。我发现最简单的解决方案是将搜索栏放置在另一个UIView中,该UIView具有所需的宽度。


为此挣扎了几个小时。将其包装在UIView中,瞬间解决!记得要包装起来哦! - lionpants
一个很棒的回答!我的完整代码: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

5
搜索栏的框架被UIKit更改了,所以我自己改变了搜索栏的框架。我在下面的委托中更改了搜索栏的框架。其中一个是UISearchBar的委托:
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
}

另一个是 UISearchDisplayController 的代理:
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
    [controller.searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
    [self.searchDisplayController.searchResultsTableView setDelegate:self];
}

它可以工作,我可以得到正确的框架,但是当我点击搜索栏时,它会抖动一下。

这不是最好的方法,但它可以工作。有没有更好的方法?

更新: 我已经调试了几个小时的UISearchBar和UISearchDisplayController,但是它有一个小错误:当我结束编辑searchBar时,它的宽度将变为320px,然后将变成我的宽度。我无法更改取消按钮的背景颜色。所以我编写了一个自定义SearchDisplayController,带有一个UISearchBar属性和一个UITableView属性。它对我很有效。


0

调用委托方法

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    [controller.searchBar setFrame:CGRectMake(0,31, 320 , 43)];
    [self.searchDisplayController.searchResultsTableView setDelegate:self];
} 

-1

您可以使用- (void)setShowsCancelButton:animated:来处理搜索栏的取消按钮。如果您不想显示取消按钮(因为取消按钮会改变搜索栏的框架),只需在委托中编写以下内容。

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{   

    [searchBar setShowsCancelButton:NO animated:YES];
}

更新:

唯一可能的解决方案似乎是从SearchBar视图层次结构中找到取消按钮并将其隐藏。

for (UIView *possibleButton in searchBar.subviews)
{
    if ([possibleButton isKindOfClass:[UIButton class]])
    {
        UIButton *cancelButton = (UIButton*)possibleButton;
        cancelButton.hidden = YES;
        break;
    }
}

谢谢您的回答,但它不起作用...尽管我设置了setShowsCancelButton:NO,取消按钮仍然显示。并且searchBar的框架也被改变了。 - cloosen
非常感谢,我在beginEditing代理和endEditing代理中将cancelButton设置为隐藏,但是cancelButton会显示一个shink然后隐藏。我认为UISearchDisplayController存在一些错误(可能是特性),因此我编写了自定义SearchDisplayController,它对我很有效。再次感谢。 - cloosen
您可以在ViewDidLoad中或者在创建SearchDisplayController之前的任何地方完成该操作。 - Animesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接