How to undo the keyboard in UISearchbar?

I use UISearchBarin my application, and it serves both for the edit field and for the search. Therefore, when I want to disappear from the keyboard, I have to use the cancel button in the UISearchBar, but I cannot have this cancel button on the screen, since T can make the keyboard disappear if not used without the cancel button. Please help me as I am new to iPhone app development.

Thanks in advance!

+3
source share
3 answers

Use this:


[UISearchBar resignFirstResponder];

Just replace the word UISearchBar with the name of the object you created.

+9
source

? , [UISearchBar resignFirstResponder]. , , , UIGestureRecognizer , , .

+1

just all you have to do is get the UITextfield control from the UISearchbar and then set the UITextfield delegate to any delegate that executes -(void) textFieldShouldReturn:(UITextField *)textField

-(void)viewDidLoad{    
    UIView * subView;
    NSArray * subViews = [searchbar subviews];
    for(subView in subViews)
    {
        if( [subView isKindOfClass:[UITextField class]] )
        {
           ((UITextField*)subView).delegate=self;
            ((UITextField*)subView).returnKeyType=UIReturnKeyDone;
            break;
        }
    }
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return TRUE;
}
+1
source

Source: https://habr.com/ru/post/1787259/


All Articles