在我正在處理的應用程序中,我現在的目標是在鍵盤顯示時滾動內容並允許用戶在顯示時滾動。我嘗試了幾種不同的解決方案,但還沒有能夠實現這一點。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;
}
,我不會感到驚訝,如果這是保持滑倒我的腦海裏那些簡單的錯誤之一,但這種輔助功能的將是非常不錯的在應用程序中。任何幫助將不勝感激,甚至其他可能的解決方案,我試圖解決的問題也會很好。
你在你的界面inlcude'UIScrollViewDelegate'? – ant
哇。當然這會是這樣簡單的事情 - 謝謝!它很高興有第二套眼睛。 –
新問題雖然,當我重新啓用水龍頭關閉鍵盤代碼時,由於點擊操作,滾動不起作用。我將如何獲得應用程序來分辨差異? –