2012-06-25 23 views
1

在我正在處理的應用程序中,我現在的目標是在鍵盤顯示時滾動內容並允許用戶在顯示時滾動。我嘗試了幾種不同的解決方案,但還沒有能夠實現這一點。UIScrollView未收到滾動動作

我使用的應用程序故事板,這裏是視圖控制器內的元素層次:

視圖控制器 的UIScrollView 的UIView 按鈕/文本框/標籤/ UIPickerView

我首先設置UIScrollView的內容大小與其內部包含所有表單元素的視圖的大小相同。如果這樣做不起作用,我試圖通過手動將內容的高度設置爲320 x 2000來實現。再一次,這不起作用。我也將滾動視圖中的用戶交互功能設置爲YES。這是我現在在這裏的代碼。

CGSize contentSize = CGSizeMake(320, 2000); 
[self.scrollView setContentSize:contentSize]; 

在滾動視圖我有一個按鈕,位於有一個動作,如果用戶觸摸它外面關閉鍵盤整個窗體後面。我禁用了這一點,看看它是否會阻止滾動的事件發生衝突。再次,沒有工作。

-(IBAction)closeKeyboard:(id)sender 
{ 
    if(![self isFirstResponder]){ 
     [self.view endEditing:YES]; 
    } 
} 

我甚至設置了一些觀察者,看看鍵盤即將出現還是消失。根據鍵盤當前所在的位置,觀察者可以調整滾動視圖的高度,而不是內容大小,只是滾動視圖本身的高度。所以在這一點上,滾動視圖中的內容將比滾動視圖本身高得多,但仍然沒有發生滾動。

這裏是我的觀察員代碼:

// adjust view based on keyboard 
- (void)keyboardWillHide:(NSNotification *)n 
{ 
    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 


    // resize the scrollview 
    CGRect viewFrame = self.view.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    //viewFrame.size.height += keyboardSize.height; 

    CGRect scrollRect = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, viewFrame.size.height + keyboardSize.height + 100); 

    [UIScrollView beginAnimations:nil context:NULL]; 
    [UIScrollView setAnimationBeginsFromCurrentState:YES]; 
    [UIScrollView setAnimationDuration:0.3]; 
    [self.scrollView setFrame:scrollRect]; 
    self.scrollView.userInteractionEnabled = YES; 
    [UIScrollView commitAnimations]; 

    keyboardShowing = false; 
} 

- (void)keyboardWillShow:(NSNotification *)n 
{ 
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown. 
    if (keyboardShowing) { 
     return; 
    } 

    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    // resize the noteView 
    CGRect viewFrame = self.view.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 

    CGRect scrollRect = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, viewFrame.size.height - keyboardSize.height - 100); 
    //scrollView.frame.size.height -= keyboardSize.height; 

    //viewFrame.size.height -= keyboardSize.height; 
    [UIScrollView beginAnimations:nil context:NULL]; 
    [UIScrollView setAnimationBeginsFromCurrentState:YES]; 
    [UIScrollView setAnimationDuration:0.3]; 
    [self.scrollView setFrame:scrollRect]; 
    self.scrollView.userInteractionEnabled = YES; 
    [UIScrollView commitAnimations]; 

    keyboardShowing = YES; 
} 

,我不會感到驚訝,如果這是保持滑倒我的腦海裏那些簡單的錯誤之一,但這種輔助功能的將是非常不錯的在應用程序中。任何幫助將不勝感激,甚至其他可能的解決方案,我試圖解決的問題也會很好。

+2

你在你的界面inlcude'UIScrollViewDelegate'? – ant

+0

哇。當然這會是這樣簡單的事情 - 謝謝!它很高興有第二套眼睛。 –

+0

新問題雖然,當我重新啓用水龍頭關閉鍵盤代碼時,由於點擊操作,滾動不起作用。我將如何獲得應用程序來分辨差異? –

回答

0

看起來您正在IB中使用手勢識別器來檢測輕敲外部事件。由於此識別器位於層次結構的最高視圖中,因此它會覆蓋滾動視圖的檢測器。您可能需要稍微改變它,具體取決於您想要點擊哪些區域來關閉鍵盤。

這是UIResponder class reference。它列出了您的視圖控制器自動繼承的所有UIResponder事件。什麼可以解決你的問題是繼承你的UIScrollView並添加鍵盤結束代碼。另外,請確保將其設置爲第一響應者。

最終代碼的工作:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 
    if ([touch tapcount] == 1) 
     for(UIView *view in self.view.subviews){ 
      if([view isKindOfClass:[UITextField class]]){ 
       [view resignFirstResponder]; 
      } 
     } 
    } 
} 
+0

我添加了您的代碼並進行了一些調整以匹配我的視圖設置,但仍然沒有任何變化。觸摸拖動仍然關閉鍵盤,而不是滾動視圖。 –

+0

您將代碼添加到/的哪個類是原始代碼?另外,你是否刪除了手勢識別器? – Dustin

+0

這不是一個手勢識別器,它是一個坐在所有其他元素後面的按鈕。無論哪種方式,是的,我刪除了。該代碼位於我正在使用的視圖控制器中。是否有可能讓程序區分同一空間中類似的事件類型? –