iOS中UITableView的UISearchDisplayController框架

4
  • 我有一个UITable,其中包含一个UISearchDisplayController
  • UITable的宽度小于屏幕宽度(它是280像素的宽度居中)。
  • 当我点击search bar时,UISearchDisplayController表格位于屏幕左侧。
  • 即使更改UISearchDisplayController表格的框架,我仍然会得到相同的定位。

我在这里设置框架:

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView  {
    tableView.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:30.0/255.0 blue:32.0/255.0 alpha:1];
    tableView.rowHeight = self.myTable.rowHeight;
    tableView.frame = myTable.frame;
    NSLog(@"search table origin: %f",tableView.frame.origin.x);
}

更奇怪的是,当我在结尾记录搜索表的位置时,它显示为16。然而,在视图中它位于位置0。

感谢您的任何帮助。


这个来自Tom Swift的答案解决了我的问题:https://dev59.com/tU_Ta4cB1Zd3GeqPE-g5#4128659 - Natan R.
2个回答

20

我回答了自己的问题。需要在这个委托方法中设置框架:

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView  {

    tableView.frame = self.myTable.frame;

}

谢谢,我本来打算自己创建一个表视图,因为我想把它稍微移动到搜索栏下面,但找不到一个干净的方法来实现。 - Oscar Gomez
1
谢谢,这确实帮了我不少,但还有至少两个问题没有解决:当旋转设备时,栏适应整个屏幕宽度,而关闭动画很丑陋,会再次闪烁到整个宽度。 - Tom

1

尝试这个简单的方法:

//--Search display controller frame

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];

    [tableView setFrame:CGRectMake(my_table.frame.origin.x, tableView.frame.origin.y, my_table.frame.size.width, my_table.frame.size.height)];

}

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