Programmatically activating UISearchBar blocks user interactions with it

僤鯓⒐⒋嵵緔 提交于 2019-12-21 20:50:30

问题


I have a simple table view inside a navigation controller with the toolbar visible. On the toolbar is a search button, and the table view's table header view is set to my UISearchBar. If I scroll the table view up and then tap on the search bar to activate it, everything happens normally. But if I tap on the search button, which activates search bar programmatically, then all interactions with the search bar are disabled (looks like it's blocked by something), and the user can't clear the search, cancel the search or even navigate around the text in the textfield.

-(void)viewDidLoad
{
    [self setupSearchBar];
}

-(void)setupSearchBar
{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeDefault;
    self.searchBar.delegate = self;
    self.tableView.tableHeaderView = self.searchBar;

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

    [self.searchController.searchResultsTableView registerNib:[UINib nibWithNibName:@"ItemCell" bundle:nil]
                                   forCellReuseIdentifier:CellIdentifier];

    for (UIView* subview in self.searchBar.subviews) {
        if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField*)subview setKeyboardAppearance:UIKeyboardAppearanceAlert];
            [(UITextField*)subview setReturnKeyType:UIReturnKeyDone];
        }
    }

    self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height);
}

-(IBAction)search:(id)sender
{
    [self.searchBar becomeFirstResponder]; // also tried [self.searchController setActive:YES animated:YES];
} 

I've set up my search display controller with an NSFetchedResultsController like described in this SO post.

UPDATE

I worked around this problem by making my UITableView scroll to the top before setting the UISearchBar to first responder. Why does this work? I don't know, seeing that the becoming first responder part is not an issue, but if the search bar is not on the screen when it becomes first responder, the cancel and clear buttons won't work. This is my code now.

-(void)search:(id)sender
{
    [self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top) animated:NO];
    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.searchController setActive:YES animated:YES];
        [self.searchController.searchBar becomeFirstResponder];
    });
}

If someone has any insights as to why this works, I'm all ears.


回答1:


Try performing a hit test, to see if your view is obstructed.

[self.view hitTest:[self.view convertPoint:searchBar.center fromView:searchBar]];



来源:https://stackoverflow.com/questions/19688500/programmatically-activating-uisearchbar-blocks-user-interactions-with-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!