resign keyboard when losing focus on uisearchbar

前端 未结 2 1416
一向
一向 2021-01-18 02:22

I\'m making an UISearchBar option in the navigationbar of my app. My app consists of multiple views and subviews. I have this mainview which has 3 other views on himself. on

相关标签:
2条回答
  • 2021-01-18 03:03

    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.

    0 讨论(0)
  • 2021-01-18 03:08

    Have you tried this,

    UISearchBar *searchBar;
    

    Next set the Getter and Setter Property of UISearchBar.

    and call the any method

    [searchBar resignFirstResponder];
    
    0 讨论(0)
提交回复
热议问题