user1480139
user1480139

Reputation: 383

UISearchbar do something before the keyboard shows up

I'm having a UISearchBar. Now when I click on the searchbar the keyboard shows up.

I need to do something before my keyboard shows up. Changes in my layout.

So is there any way to do something before the keyboard shows up?

I thought maybe like this but it doesn't work:

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [_searchBar resignFirstResponder];

    return YES;
}

But the keyboard doesn't hide.

Any one suggestions?

Upvotes: 1

Views: 829

Answers (4)

Miraslau
Miraslau

Reputation: 548

You can do this using the following standard iOS notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown) name:UIKeyboardWillShowNotification object:nil];

Upvotes: 0

Vinay Mishra
Vinay Mishra

Reputation: 406

if you want to hide keyboard : use only this viewwillAppear method on

[searchBarObje resignFirstResponsder];

You can simply call : for showing

[searchBarObje becomeFirstResponsder];

or you can use 
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
   // Do the changes in UI
   [searchBar becomeFirstResponsder];
    return YES;
}

I think ., This will help you.

Upvotes: 0

Jach0
Jach0

Reputation: 115

You can use NSNotificationCenter to get notified when the keyboard will show and hide.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

Then add the two methods that will get called:

- (void)onKeyboardWillShow:(NSNotification *)notification
{   
    NSLog(@"Keyboard will show!");
}

- (void)onKeyboardWillHide:(NSNotification *)notification
{
    NSLog(@"Keyboard will hide!");
}

And don't forget to stop listening when leaving the view:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

Upvotes: 0

jithinroy
jithinroy

Reputation: 1885

You can do the UI changes in searchBarShouldBeginEditing before returning YES or NO. If you dont want to show the keyboard return NO.

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
   // Do the changes in UI

    return YES;
}

Upvotes: 1

Related Questions