resign keyboard when losing focus on uisearchbar

你离开我真会死。 提交于 2019-12-01 16:36:16

Solved it!

- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.myViewController1.customView1 setUserInteractionEnabled:NO];
    [self.myViewController2.customView2 setUserInteractionEnabled:NO];   
    [searchBar setShowsCancelButton:YES animated:[mySettings animation]];
    return YES;   
}

I started by shutting down the userInteraction on my 2 subviews, at the moment I start using the searchBar.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [[event allTouches] anyObject];
    if ([self.mySearchBar isFirstResponder] && [touch view] != self.mySearchBar) 
    {
        [self.mySearchBar resignFirstResponder];
        [self.myViewController1.customView1 setUserInteractionEnabled:YES];
        [self.myViewController2.customView2 setUserInteractionEnabled:YES];
    }
    [super touchesBegan:touches withEvent:event];
}

Then when I click/tap/touch outside the searchBar I first resign the keyboard which is first responder still AND AFTER that I set userInteraction back on for the 2 subviews. The order of doing this is vital!

This piece of code allows u to resign the keyboard in a single mainViewController even when it is crowded with a massload of subviews.

user2747071

Have you tried this,

UISearchBar *searchBar;

Next set the Getter and Setter Property of UISearchBar.

and call the any method

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