2012-04-29 23 views
1

我讀了很多的話題,但我不能找到:-(也許一個解決方案可以幫助鍵盤出現在iPad應用程序中時,在工具欄中滾動UISearchBar?

在我的故事板(用於iPad應用程序),我有以下幾點: - 導航控制器,這是我最初的場景 - 一個Segue公司打開一個包含以下層次視圖控制器: - 滾動查看 - >工具欄 - >欄按鈕項目 - >搜索欄進行提示 - >地圖視圖 - 導航項目 - >欄按鈕項

我的搜索欄位於底部olbar,因此當我點擊它時,顯示鍵盤並隱藏我的搜索欄。我想滾動一切顯示我的搜索欄(導航,地圖,工具欄...)。

我已經註冊鍵盤通知和執行的代碼作爲IOS文檔中描述

- (void)keyboardWasShown:(NSNotification*)aNotification 

{ 的NSDictionary *信息= [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]; 
} 

}

當執行這段代碼,似乎有事(我的地圖視圖中顯示一個白色視圖,而不是地圖),並顯示鍵盤,但我的搜索欄仍然是無形的。

我的視圖層次結構中是否存在問題?因爲它嵌入在導航控制器中?我對這個問題有點失落。只是一點,在我的層次結構中有一個scrollView之前,我有一個簡單的View,只是用滾動視圖取代了簡單的視圖,因爲搜索欄的問題。 您的幫助將非常感謝! 謝謝,

Sébastien。

+0

我已經進步了分析一點點,在scrollPoint的計算問題。 如果我使用:CGPoint scrollPoint = CGPointMake(0.0,352.0);有用。使用activeField的任何想法都不會返回適當的值? activeField是我應用程序底部工具欄中的UISearchBar。謝謝! – sebastien 2012-04-29 18:19:41

回答

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

}

相關問題