iPhone键盘隐藏搜索栏

时间:2011-09-16 06:21:25

标签: iphone ios uisearchbar uisearchdisplaycontroller uisearchbardisplaycontrol

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

有没有办法在键盘出现时移动搜索栏?任何代码片段都会非常有用。

3 个答案:

答案 0 :(得分: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: 如果您不想隐藏搜索栏后面的视图,则只要搜索栏向上移动,您就必须缩小其高度。它可以与代码相同的位置来设置搜索栏的动画。

答案 1 :(得分: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];
    }

答案 2 :(得分:-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

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

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