1

我在视图底部有一个搜索栏。问题是每当我在键盘上输入内容时,搜索栏仍然保留在底部,我无法查看我正在输入的内容。我想在键盘弹出时动态向上移动它,然后在键盘消失后收回其原始位置。不幸的是,我的应用程序的搜索栏必须位于底部,并且有点卡在这个位置。

有没有办法只在键盘出现时才向上移动搜索栏?任何代码片段都会非常有帮助。

4

3 回答 3

1

您最好的选择可能是在 UISearchBarDelegate 协议中使用 –searchBarShouldBeginEditing:。

它看起来像这样:

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

编辑:其他答案之一建议使用 UIKeyboard 通知,但这可能会令人困惑。然而,它确实提供了每次键盘出现时工作的优势,而不仅仅是当 UISearchBar 是第一响应者时。

编辑 2:Mayur Joshi 建议使用动画,可以这样做:

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

编辑 3:如果您不想遮挡搜索栏后面的视图,则每当搜索栏向上移动时,您都必须缩小其高度。它可以和代码放在同一个地方来为你的搜索栏设置动画。

于 2011-09-16T06:35:19.277 回答
0

嗨,试试这个 sBar 是 UISearchBar 对象。

    - (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];
    }
于 2011-09-16T06:35:06.327 回答
-1

您将不得不使用 UIScrollView 并将此搜索栏保留在该滚动视图中。现在,当您开始编辑搜索栏时,即

 - (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar

为 UIScrollView 设置 scrollRectToVisible 方法,以使您的 SearchBar 像这样可见....

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

当你在搜索栏中写下文字时,即

 - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1

将滚动视图 scrollRectToVisible 设置为其原始大小。

[yourScrollView scrollRectToVisible:CGRectMake(0, 0, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];  
于 2011-09-16T06:33:21.073 回答