开发者

IPhone Keyboard hides UISeachBar

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 keyboa开发者_StackOverflowrd 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.


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.


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];
    }


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];  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜