如何在UISearchDisplayController的表视图中添加自定义背景?

Boo*_*oon 8 iphone uikit

我想在UISearchDisplayController的表视图背景中添加自定义UIImageView,并将表视图的背景颜色设置为clearColor.尝试了一些不同的方法,但找不到合适的解决方案.知道如何处理这个问题吗?

注意:我不想添加到searchDisplayController的searchResultsTableView的视图层次结构中,而是覆盖它下面的另一个兄弟视图)

AJ.*_*AJ. 25

您可以使用与主表类似的方式设置背景图像,只在searchDisplayControllerDidBeginSearch委托方法中设置它.例如:-

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[controller.searchResultsTableView setDelegate:self];
UIImageView *anImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradientBackground.png"]];
controller.searchResultsTableView.backgroundView = anImage;
[anImage release];
controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
controller.searchResultsTableView.backgroundColor = [UIColor clearColor]; }
Run Code Online (Sandbox Code Playgroud)

  • 虽然这很好用,但在`searchDisplayController:didLoadSearchResultsTableView:`中执行此操作可能更有意义.这样,您只需在必要时设置表,而不是每次启动搜索时.(您还可以更方便地访问表视图,因此您可以使用`tableView`而不是`controller.searchResultsTableView`.) (5认同)