[英]TextField attach to keyboard while editing - how to make the ScrollView appear same as before editing?
我創建了新的Xcode單視圖項目。 在界面生成器中,我添加了UIScrollView來覆蓋完整視圖。 在此滾動視圖上,我添加了UITextField。 生成的UI如下所示:
請注意,滾動視圖此時不會滾動,因為內容僅采用視圖的大小,而不會大於視圖的大小。
現在,為了在編輯時將UITextField帶到鍵盤上方,我按照Apple在“ 管理鍵盤”頁面上描述的方法進行操作。 完成此操作后,它給了我預期的行為,使編輯時鍵盤上方的文本字段開始,如以下屏幕截圖所示:
現在,在調用[textfield endEditing:YES]
,鍵盤將隱藏起來,但是文本字段不會返回到其原始位置。 它返回到比其原始位置稍高的位置,現在滾動視圖變為可滾動的,就好像添加了一點高度:
注意上面截圖中的滾動條!
我需要幫助,以在編輯結束后(當鍵盤隱藏時)恢復視圖的原始行為,即textField返回完全相同的位置並滾動,因為在編輯開始之前未發生滾動。
You need adjust scrollview contentOffset textFieldDidBeginEditing and textFieldDidEndEditing.
or
One controller is available for scrollview auto scroll.
我認為,Apple 在此指定的官方方式是使此功能正常工作的最簡便和最佳方法。
您也可以執行類似的操作,而無需使用鍵盤通知。 您可能知道我們有TextField
委托方法,我們可以使用它們來設置scrollView contentOffset並獲得相同的行為
- (void)textFieldDidBeginEditing:(UITextField *)textField{
scrollView.contentOffset = CGPointMake(0, textField.center.y-80); // you can change 80 to whatever which fits your needs
}
上面的方法設置滾動視圖的contentOffset
值,並且textFiled
向上移動,而textField
resignFirstResponder
則調用以下委托方法,您可以在其中重新設置contentOffset
值
- (void)textFieldDidEndEditing:(UITextField *)textField{
scrollView.contentOffset = CGPointMake(0,-80);
}
注意:您需要使視圖中的每個文本字段都具有其委托作為UIViewController
實例。 您還需要UIViewController
來采用UITextFieldDelegate
雖然不是最好的代碼,但是它具有您可以使用的更多功能,帶有_的任何內容都是全局變量
//Handle notification when keyboard appear
- (void)keyboardOnScreen:(NSNotification *)notification
{
if (_isKeyboardShow) {
return; //If keyboard is showing then return
}
_keyboardHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[self animateTextFieldUp: YES];
_isKeyboardShow = YES;
}
//Handle notification when keyboard hide
- (void)keyboardOffScreen:(NSNotification *)notification
{
if(!_isKeyboardShow) return;
[self animateTextFieldUp: NO];
_isKeyboardShow = NO;//Missed this line
}
//Push view up with animation when keyboard show
- (void) animateTextFieldUp: (BOOL) up
{
UITextField *textfield = [UIResponder currentFirstResponder];
CGPoint windowPoint = [textfield convertPoint:textfield.bounds.origin toView:self.view];
int movementDistance;
CGPoint point = [_mainScrollView contentOffset];
//Push up only when blocked by keyboard
if (windowPoint.y + textfield.frame.size.height >= self.view.frame.size.height - _keyboardHeight) {
movementDistance = windowPoint.y - (self.view.frame.size.height - _keyboardHeight) + textfield.frame.size.height + 10;
_oldMovementDistance = movementDistance;
int movement = (up ? -movementDistance : movementDistance);
[_mainScrollView setContentOffset:CGPointMake(0, point.y - movement) animated:YES];
}
else { //Push view down the same amount
int movement = (up ? -movementDistance : _oldMovementDistance);
[_mainScrollView setContentOffset:CGPointMake(0, point.y - movement) animated:YES];
_oldMovementDistance = 0;
}
}
看一下您的代碼,嘗試定位滾動視圖時無需更改內容插圖等。 您只需要修改內容偏移屬性。
這是修改后的代碼:
@interface ViewController () {
UITextField *activeField;
CGPoint scrollViewOldPosition;
}
如下修改keyboardWasShow:
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat someSpaceBetweenKeyBoardAndField = 20.0;
scrollViewOldPosition = self.scrollView.contentOffset;
self.scrollView.contentOffset = CGPointMake(0, kbSize.height - (self.view.frame.size.height - activeField.frame.origin.y - activeField.frame.size.height) + someSpaceBetweenKeyBoardAndField);
}
鍵盤將被隱藏的方法:
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
self.scrollView.contentOffset = scrollViewOldPosition;
}
在編輯過程中將UITextField和UITextView移出鍵盤:
對於非UITableViewControllers,將TPKeyboardAvoidingScrollView.m
和TPKeyboardAvoidingScrollView.h
源文件拖放到您的項目中,將UIScrollView彈出到視圖控制器的xib或Storyboard中,將滾動視圖的類設置為TPKeyboardAvoidingScrollView,然后將所有控件放入該滾動視圖中。 您也可以以編程方式創建它,而無需使用xib-只需將TPKeyboardAvoidingScrollView用作頂層視圖即可。
要與UITableViewController類一起使用,請將TPKeyboardAvoidingTableView.m和TPKeyboardAvoidingTableView.h放到您的項目中,並使UITableView成為xib中的TPKeyboardAvoidingTableView。 如果您的控制器沒有使用xib,我不知道有什么簡單的方法可以使其UITableView成為自定義類:阻力最小的路徑是為其創建xib。
您可以從這里獲取參考。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.