1

I have a search bar at the bottom of a view. The issue is whenever I type something in the keyboard, the search bar still remains at the bottom and I cannot view what I am typing. I would like to dynamically move it up whenever the keyboards pops up and then take back its original position after the keyboard disappears. And unfortunately the search bar has to be in the bottom for my app and kind of stuck in this.

Is there any way around to move the search bar up only when the keyboard appears? Any code snippets would be really helpful.

Frooti
  • 37
  • 5

3 Answers3

1

Your best bet is probably to use the –searchBarShouldBeginEditing: in the UISearchBarDelegate protocol.

It would look something like this :

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    CGRect newFrame = //some rect
    mySearchBar.frame = newFrame;
    return YES;//this is important!
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    CGRect originalFrame = //the original frame
    mySearchBar.frame = originalFrame;
    return YES;
}

EDIT: One of the other answers suggests using the UIKeyboard notifications, but that can be confusing. It does, however, give the advantage of working for every time the keyboard appears, rather than just when the UISearchBar is the first responder.

EDIT 2: Mayur Joshi suggests using animation, which can be done like so:

[UIView animateWithDuration:duration
            animations:^{ 
                //what you want to animate (in this case, the search bar's frame)
            } 
            completion:^(BOOL finished){
                //what to do when the animation finishes
            }];

EDIT 3: If you don't want to obscure the view behind the search bar, you will have to shrink its height whenever the search bar moves up. It can go in the same place as the code to animate your search bar.

Jumhyn
  • 6,687
  • 9
  • 48
  • 76
  • if there is a SearchBar at the bottom of the View it should stick there only. SearchBar alone roaming here and there while the remaining View stuck there is Bad UI! – mayuur Sep 16 '11 at 06:49
  • That is what Frooti asked to do. Whether or not it is a bad HI decision is not part of the question. I have addressed your comment about animations in the 2nd edit. – Jumhyn Sep 16 '11 at 06:53
  • 1
    Well, the answer lies there. Doing something with a bad UI or good UI is his decision, obviously. – mayuur Sep 16 '11 at 06:58
  • But it is not necessarily a bad UI decision. If the user is editing in the search bar that is clearly the thing that they are going to be using right then, so obstruction of the behind view is not an issue. Even so , I have updated my answer to fix it again. – Jumhyn Sep 16 '11 at 07:03
  • No offence to your answers but UI is the main criteria when u develop iPhone apps. shrinking your view may not be considered bad UI, but when you see most of the successful iPhone apps, and when you select any Textfield or search bar or anything, the whole View and not that particular element scrolls down or animates down(doesnt shrink). Hence, scrolling the view downwards is more acceptable. – mayuur Sep 16 '11 at 07:11
  • Changing the height doesn't actually change how the view looks. But I suppose moving the origin up works too. – Jumhyn Sep 16 '11 at 07:18
0

Hi, try this sBar is UISearchBar object.

    - (void)keyboardWillShow:(NSNotification *)aNotification 
    {
        // the keyboard is showing so resize the table's height
        CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame = sBar.frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        frame.origin.y=frame.origin.y- keyboardRect.size.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        sBar.frame = frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        [UIView commitAnimations];
    }

    - (void)keyboardWillHide:(NSNotification *)aNotification
    {
        // the keyboard is hiding reset the table's height
        CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame = sBar.frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        frame.origin.y=frame.origin.y+ keyboardRect.size.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        sBar.frame = frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);

        [UIView commitAnimations];
    }
TMB
  • 4,683
  • 4
  • 25
  • 44
Gypsa
  • 11,230
  • 6
  • 44
  • 82
-1

You will have to use a UIScrollView and keep this search bar in that scroll View. Now, when you start editing the Search Bar, that is,

 - (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar

set the scrollRectToVisible method for the UIScrollView so as to make your SearchBar visible like this....

[yourScrollView scrollRectToVisible:CGRectMake(0, 300, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];    

And when you have written the text in the search bar, i.e

 - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1

set the Scroll view scrollRectToVisible to its original size.

[yourScrollView scrollRectToVisible:CGRectMake(0, 0, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];  
mayuur
  • 4,736
  • 4
  • 30
  • 65
  • A UIScrollView is not necessary for this, you can simply set the frame. – Jumhyn Sep 16 '11 at 06:38
  • but then bringing up the frame above and then throwing it down on keyboard disappear and not animating it, makes up an AWFUL UI! NOT RECOMMENDED. Also, if you do animation only with the SearchBar, there will be the searchBar only roaming here and there. Again, AWFUL UI! – mayuur Sep 16 '11 at 06:46
  • I have addressed your complaint about animation in an edit above. – Jumhyn Sep 16 '11 at 06:57
  • You have animated just the Search Bar. Either you'll need to animate the whole View or just use the scrollView. – mayuur Sep 16 '11 at 07:00