1

我阅读了很多主题,但找不到解决方案:-(也许您可以提供帮助!

在我的故事板(适用于 iPad 应用程序)中,我有以下内容: - 作为我的初始场景的导航控制器 - 打开包含以下层次结构的视图控制器的 Segue: - 滚动视图 -> 工具栏 -> BAR 按钮项 -> 搜索带提示的栏 -> 地图视图 - 导航项 -> 栏按钮项

我的搜索栏位于底部工具栏中,因此当我单击它时,会显示键盘并隐藏我的搜索栏。我想向上滚动所有内容以显示我的搜索栏(导航、地图、工具栏...)。

我已经注册了键盘通知并按照 IOS 文档中的描述实现了代码

- (void)keyboardWasShown:(NSNotification*)aNotification

{ NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
}

}

执行此代码时,似乎发生了一些事情(我的地图视图显示白色视图而不是地图)并且显示了键盘,但我的搜索栏仍然不可见。

我的视图层次结构中有问题吗?因为它嵌入在导航控制器中?我对这个问题有点迷茫。只是一点,在我的层次结构中有一个滚动视图之前,我有一个简单的视图,由于搜索栏的问题,只是用滚动视图替换了简单视图。您的帮助将不胜感激!谢谢,

塞巴斯蒂安。

4

1 回答 1

0

我发现我的错误,这是因为我的应用程序处于横向模式并且 uiSearchBar 位置是相对于工具栏的。下面是修复

- (void)keyboardWasShown:(NSNotification *)aNotification {

// Get the Keyboard size
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

// Add to the scroll view a bottom inset equal to height of the keyboard
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.width; // in landscape mode need to get width and not height!

// Compute the position of the uiSearchBar in the screen (whole scrollview)
CGPoint realPoint = [_LocateCell convertPoint:_LocateCell.frame.origin toView:self.scrollView];

if (!CGRectContainsPoint(aRect, realPoint) ) {
    CGPoint scrollPoint = CGPointMake(0.0, kbSize.width);
    [self.scrollView setContentOffset:scrollPoint animated:YES];
}

}

于 2012-04-30T19:58:25.537 回答