IPhone Keyboard hides UISeachBar

左心房为你撑大大i 提交于 2020-01-05 04:08:15

问题


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.


回答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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/7440889/iphone-keyboard-hides-uiseachbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!