UISearchBar自动调整大小并更改帧

时间:2014-01-07 13:44:16

标签: ios uisearchbar uisearchdisplaycontroller

我遇到一个搜索栏的问题,当搜索栏变为firstResponder并且当它停止时,它会以奇怪的方式运行。

搜索栏被添加为表格视图的标题

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.tableView.tableHeaderView = self.searchBar;

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
                                                          contentsController:self];
self.searchController.searchResultsDataSource = self;

视图控制器设置为JASidePanelController的左侧面板,当键盘显示或隐藏时,它会隐藏中央面板:

- (void)keyboardWillAppear:(NSNotification *)note
{
    [self.sidePanelController setCenterPanelHidden:YES
                                          animated:YES
                                          duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    self.searchBar.showsCancelButton = YES;
}

- (void)keyboardWillDisappear:(NSNotification *)note
{
    [self.sidePanelController setCenterPanelHidden:NO
                                          animated:YES
                                          duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    self.searchBar.showsCancelButton = NO;
}

正常状态 Normal state

当搜索栏变为firstResponder时,它会向上移动一个点或随机向下移动

Point up Point down



当搜索栏退出时,它会动画到达窗口原点,然后返回其自然帧

Stretching up

Reaching origin

这是一个sample project再现错误。

编辑:

根据@kwylez suggestion,可以通过以下方式避免搜索栏在重新签名时产生的不需要的动画:

self.searchBar.clipsToBounds = YES;

3 个答案:

答案 0 :(得分:8)

我通过创建一个ClipBounds设置为YES的UIView解决了这个问题,然后在其中添加了子视图搜索栏。 然后将其包含在tableview标题中。它现在正在工作。

由于

答案 1 :(得分:0)

使用搜索栏和视图控制器初始化搜索显示控制器,视图控制器负责管理要搜索的数据。当用户开始搜索时,搜索显示控制器将搜索界面叠加在原始视图控制器的视图上,并在其表视图中显示搜索结果。

自定义搜索栏视图

Fixed - UISearchBar-bug-master

答案 2 :(得分:-1)

我将问题追溯到JASidePanelController中的函数“_layoutSidePanels”。

在你的app委托中,我注释掉了以下代码,它似乎解决了灰色视图不断增长和缩小的问题。

rootViewController.shouldResizeLeftPanel = YES;

如果您遵循代码,当选择搜索栏时,您调用setCenterPanelHidden,随后调用_layoutSidePanels,它运行以下代码:

if (self.leftPanel.isViewLoaded) {
    CGRect frame = self.leftPanelContainer.bounds;
    if (self.shouldResizeLeftPanel) {
        frame.size.width = self.leftVisibleWidth;
    }
    self.leftPanel.view.frame = frame;
}

更改侧面板的框架似乎是原因,正如我所说,评论代码修复了我的问题。

编辑:最初看起来似乎搜索栏在一个点上下移动,但是经过进一步检查,它似乎总是略微位于导航栏下方,但是在您选择搜索栏之前您没有注意到它并且视图的其余部分为“灰色”,因此蓝色导航栏和浅灰色搜索栏之间的白色小空间变为深灰色,就像下面的桌面视图的其余部分一样。

编辑#2:花了我一会儿,但我设法找出灰色面具的来源。您的UISearchDisplayController负责在搜索栏成为第一响应者时出现的灰色背景,当我删除以下两行代码时,您看到的问题就消失了:

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;

这样做只是为了说明问题的原因,但删除这些代码行将禁用使用搜索显示控制器获得的任何功能。我不知道你到底希望做什么,所以我真的不能给你任何关于如何继续的建议,但希望我已经指出你的原因正确的方向!