【发布时间】:2014-02-27 09:20:13
【问题描述】:
背景: 我有一个 UIViewController,它以编程方式添加了一个 UITableView。在 UITableView 的顶部,我有一个 tablHeaderView,我在其中放置了一个 UIView。这个 UIView 有一个 UISearchBar 和一个 segmentedControl。想法是:用户可以按一些基本类别(例如“日期/时间”或“位置”等)对 UITableView 进行排序。他们还可以按程序中的项目进行搜索。
问题:当我点击搜索栏时,它会调整大小(我不想要),然后当我取消搜索时,它会再次调整大小并停留在那里,直到 UIViewController 退出并再次加载。
代码:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(@"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Date/Time", @"Location", @"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
我尝试过的:我尝试过设置 SearchBar 的大小,而不是设置它的大小。没有它在 tableHeaderView 的 UIView 中,而只是让它自己存在。
为什么它会调整大小,我怎样才能让它停止?
编辑:我刚刚尝试了以下操作:在故事板(最初创建 UIViewController 的位置)中,我选择了有问题的 UIVC,在属性检查器中,我取消选择了“在顶部栏下”和“在底部栏,这似乎已经解决了动画问题的第一部分。现在,当我在搜索栏中点击时,搜索变为活动状态,但 searchBar 不会调整大小。但是,当我取消 searchBar 时,searchBar 仍会根据屏幕截图中的最后一张图像进行动画处理和调整大小。什么可能导致调整大小?
【问题讨论】:
-
我有同样的问题,我尝试使用这个解决方案。它工作正常。 stackoverflow.com/questions/21419992/…>
标签: ios objective-c cocoa-touch uitableview