我在視圖中嵌入了tableview。即使使用鍵盤,該嵌入式表格視圖的頂部行也始終可見。如果您向較低的單元格輸入內容,我希望它們像往常一樣向上滾動,以便查看您要輸入到文本字段中的內容。然而,這不會發生,它們仍然隱藏在鍵盤後面。我該如何改變這一點?iOS:在向文本框輸入值時,嵌入式tableview不會「向上滾動」
THX 邁克爾
我在視圖中嵌入了tableview。即使使用鍵盤,該嵌入式表格視圖的頂部行也始終可見。如果您向較低的單元格輸入內容,我希望它們像往常一樣向上滾動,以便查看您要輸入到文本字段中的內容。然而,這不會發生,它們仍然隱藏在鍵盤後面。我該如何改變這一點?iOS:在向文本框輸入值時,嵌入式tableview不會「向上滾動」
THX 邁克爾
你不免費獲得這種行爲,你需要滾動內容上/下自己。 A UITableView
繼承自UIScrollView
,因此滾動不是實際的問題。要滾動,您可以設置其contentSize
和contentOffset
(即使是喜歡的動畫也是如此)。
的第一件事是,你要聽這些通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
這些會告訴你的鍵盤是否顯示或隱藏。在每一種方法,相應地改變你的UITableView
的contentOffset
和contentSize
:
- (void)keyboardWillShow:(NSNotification *)notification
{
// keyboard info
NSDictionary *userInfo = [notification userInfo];
CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIViewAnimationOptions animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// offsets
UIView *cell = myTextField.superView; // assuming your text field is a direct child of the UITableViewCell
CGFloat textFieldBottomY = self.view.frame.origin.y + myTableView.frame.origin.y - myTableView.contentOffset.y + cell.frame.origin.y + myTextField.origin.y + myTextField.frame.size.height;
CGFloat keyboardTopY = keyboardRect.origin.y;
// new content size: add size to be able to scroll
originalContentOffset = myTableView.contentOffset; // store the original content offset to set back when keyboard hides
originalContentSize = myTableView.contentSize; // store the original content size to set back when keyboard hides
CGSize newContentSize = myTableView.contentSize;
newContentSize.height += keyboardRect.size.height;
myTableView.contentSize = newContentSize;
// scroll to just beneath your text field
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
myTableView.contentOffset = CGPointMake(0, textFieldBottomY - keyboardTopY);
} completion:NULL];
}
要重置的一切:
- (void)keyboardWillHide:(NSNotification *)notification
{
// keyboard info
NSDictionary *userInfo = [notification userInfo];
UIViewAnimationOptions animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// move content
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
myTableView.contentOffset = originalContentOffset;
} completion:^(BOOL finished) {
if (finished) {
// reset content size
myTableView.contentSize = originalContentSize;
}
}];
}
要檢測其文本字段中選擇/主動:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
myTextField = textField;
}
我沒有測試UITableView
上的代碼,但我最近在UIScrollView
上使用了它。再次 - 由於UITableView
繼承自UIScrollVIew
,這應該不成問題。如果表格視圖不能正確滾動到文本字段的下方,則textFieldBottomY
的值不正確。
這是一個link與實現這一點的詳細解釋。它解釋了UIScrollView
上的方法,該方法也將在UITableView
上運行。
如果您使用了UITableViewController
而不是在視圖中手動添加UITableView
,那麼您將自動獲得該行爲。
您必須滾動到TextField和Tableview之間的位置,請檢查answer。
用戶鍵入時必須手動滾動表格。 – Geek
Check [this](http://stackoverflow.com/questions/8706129/how-to-scroll-view-up-when-keyboard-appears)out –