我可以在UISearchDisplayController中插入一个按钮吗?

时间:2011-04-08 16:30:00

标签: iphone objective-c ios uisearchbar

当用户点击搜索栏时,没有输入任何文字,并且屏幕已经变暗(但是桌面视图还没有出现),我想在屏幕上放一个按钮以允许用户导航到其他一些功能。这可能吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

这非常棘手。 _dimmingView对searchDisplayController是私有的,它高于所有子视图。您可以做的是每次出现时都用自定义视图覆盖它([searchString length] == 0和DidBeginSearch)

(为表放在表的tableViewHeader上的UISearchBar设置了tempView的框架)

- (void)viewDidLoad {
    tempView = [[UIView alloc] initWith...];
    // tempView setup
    ...
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
    [tempView setFrame:CGRectMake(0, self.searchDisplayController.searchBar.frame.size.height, 320, self.searchDisplayController.searchResultsTableView.frame.size.height)];
    [self.searchDisplayController.searchContentsController.view addSubview:tempView];
    ...
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    if ([searchString length] == 0) 
         [self.searchDisplayController.searchContentsController.view addSubview:tempView];
    else 
         [tempView removeFromSuperview];
    ...
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    if (tempView && tempView.superview) 
        [tempView removeFromSuperview];
    ...
}

注意:我尝试在DidBeginSearch上创建一个新实例并在DidEndSearch上发布它,它只适用于第一次调用!奇怪的...

希望这有帮助