2012-06-20 157 views
1

我在iPad應用程序中滾動屏幕時遇到問題。在我的應用程序中,頁面底部可能會有很多字段。因此,取決於當前活動字段的位置,而不是頻繁調整屏幕位置,我希望屏幕位於以下兩個位置之一:滾動或不滾動。如果用戶正在使用鍵盤隱藏的任何部分的字段,我想按鍵盤高度滾動屏幕。爲什麼在向上滾動屏幕後我的屏幕向下滾動?

我的問題是,當鍵盤出現後,屏幕滾動我的代碼指定的距離,然後屏幕捕捉到一個位置,使光標,無論它發生在哪裏,只是在頂部鍵盤。我不明白是什麼導致了這個最後的捕捉。下面是我的滾動代碼(從教程中解除)。

感謝

//Scroll the screen up if the keyboard hides the current field 
- (void)keyboardDidShow:(NSNotification *)notification 
{ 
    // Step 1: Get the height of the keyboard. 
    if ([self interfaceOrientation] == UIInterfaceOrientationPortrait || [self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; 
    } 
    else 
    { 
     keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.width; 
    } 

    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height. 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0); 
    [(UIScrollView*)[self view] setContentInset: contentInsets]; 
     [(UIScrollView*)[self view] setScrollIndicatorInsets: contentInsets]; 

    // Step 3: Scroll the screen up by the height of the keyboard. 
     visibleRect = self.view.frame; 
     visibleRect.size.height -= (keyboardHeight + [[self toolbar] bounds].size.height); 
     if (([self.activeField frame].origin.y + [self.activeField frame].size.height) > visibleRect.size.height) 
     { 
      //make sure scrolling is vertical only 
      CGPoint scrollPoint = CGPointMake(currentLeftEdge, keyboardHeight); 
      [(UIScrollView*)[self view] setContentOffset: scrollPoint animated:YES]; 
     } 
} 

回答

0

你不改變對一些文本字段委託方法什麼嗎?如果沒有,看起來你沒有正確設置scrollview的內容,所以在setContentOffset:Animated:之後,scrollview會自動調整。

+0

文檔寫入的方式告訴我,scrollview不會自行調整,或者有任何自動行爲。但是當我更多地玩這個應用程序時,似乎我只在TextView上得到這種捕捉行爲。 ScrollView自動調整自己,以便當前正在編輯的TextView的行保留在鍵盤的頂部。每當光標移動到新行時,ScrollView就會向上滾動一行以將光標保持在視圖中。這是好的,只是不是我想要的,並且沒有在我正在閱讀的Apple文檔中描述。我想關閉此行爲。 –