TextField 被 iOS 虚拟键盘覆盖
TextField is covered by iOS virtual keyboard
您好,我在一个单元格中有一个 UItextField,它在开始编辑时被虚拟键盘覆盖。这是我为了说明 viewController
中的组件而编造的 JSON 表示
MyViewController : {
UIView : {
UIView : {
height : 100
},
UITableView : {
cell1 : myCell,
cell2 : myCell,
cell3 : myCell,
cell4 : {
UITextField
}
}
}
}
我应该怎么做才能在开始编辑 TextField 时向上滚动视图?
您想在键盘即将显示时调整受影响视图的内容插图。然后在键盘关闭后将其重置为 0。
标准做法是在键盘可见时使用滚动视图来滚动视图的内容。 Apple 在其文档 Managing the Keyboard:
中提供了示例代码
Listing 5-3 Adjusting the frame of the content view and scrolling a
field above the keyboard
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = activeField.superview.frame;
bkgndRect.size.height += kbSize.height;
[activeField.superview setFrame:bkgndRect];
[scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
}
注册键盘通知需要额外的步骤,所有步骤都包含在提到的文档中。
文档中还提到了其他方法,none 其中是自动的。有时,当这些基本功能需要额外的代码时,例如这,提醒我们 Apple 的开发工具还有改进的空间。
首先,当键盘出现时,您需要从 iOS 获得通知。
在 viewDidLoad 中,您可以注册您的 VC 以观察:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];
现在您的 keyboardWillShown: 方法将在键盘出现时被调用。
在 keyboardWillShown: 方法中,您要更改 tableViews contentInset。你是这样做的:
- (void)keyboardWillShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
self.keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,self.keyboardSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
[self scrollToContent];
}
现在您更改了 tableViews contentInset 并将键盘大小保存为 属性 (keyboardSize),但未检查您的内容是否可见。在最后一行中,我调用了 scrollToContent 方法,它将 tableView 滚动到所需的位置:
-(void)scrollToShowActiveField {
//your views size
CGRect myViewRect = self.view.frame;
//substract the keyboards size
myViewRect.size.height -= self.kbSize.height;
//offsettedFrame is your UITextView
CGRect offsettedFrame = self.activeField.frame;
//we want to see some "padding" between the textview and the keyboard so I add some padding
offsettedFrame.origin.y += 100;
if (!CGRectContainsPoint(myViewRect, offsettedFrame.origin) ) {
[self.tableView scrollRectToVisible:offsettedFrame animated:YES];
}
}
我假设您的 UITableView 具有全屏尺寸。如果不是,则需要根据需要调整 contentInset 大小。
您好,我在一个单元格中有一个 UItextField,它在开始编辑时被虚拟键盘覆盖。这是我为了说明 viewController
中的组件而编造的 JSON 表示MyViewController : {
UIView : {
UIView : {
height : 100
},
UITableView : {
cell1 : myCell,
cell2 : myCell,
cell3 : myCell,
cell4 : {
UITextField
}
}
}
}
我应该怎么做才能在开始编辑 TextField 时向上滚动视图?
您想在键盘即将显示时调整受影响视图的内容插图。然后在键盘关闭后将其重置为 0。
标准做法是在键盘可见时使用滚动视图来滚动视图的内容。 Apple 在其文档 Managing the Keyboard:
中提供了示例代码Listing 5-3 Adjusting the frame of the content view and scrolling a field above the keyboard
- (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; CGRect bkgndRect = activeField.superview.frame; bkgndRect.size.height += kbSize.height; [activeField.superview setFrame:bkgndRect]; [scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
}
注册键盘通知需要额外的步骤,所有步骤都包含在提到的文档中。
文档中还提到了其他方法,none 其中是自动的。有时,当这些基本功能需要额外的代码时,例如这,提醒我们 Apple 的开发工具还有改进的空间。
首先,当键盘出现时,您需要从 iOS 获得通知。
在 viewDidLoad 中,您可以注册您的 VC 以观察:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];
现在您的 keyboardWillShown: 方法将在键盘出现时被调用。 在 keyboardWillShown: 方法中,您要更改 tableViews contentInset。你是这样做的:
- (void)keyboardWillShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
self.keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,self.keyboardSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
[self scrollToContent];
}
现在您更改了 tableViews contentInset 并将键盘大小保存为 属性 (keyboardSize),但未检查您的内容是否可见。在最后一行中,我调用了 scrollToContent 方法,它将 tableView 滚动到所需的位置:
-(void)scrollToShowActiveField {
//your views size
CGRect myViewRect = self.view.frame;
//substract the keyboards size
myViewRect.size.height -= self.kbSize.height;
//offsettedFrame is your UITextView
CGRect offsettedFrame = self.activeField.frame;
//we want to see some "padding" between the textview and the keyboard so I add some padding
offsettedFrame.origin.y += 100;
if (!CGRectContainsPoint(myViewRect, offsettedFrame.origin) ) {
[self.tableView scrollRectToVisible:offsettedFrame animated:YES];
}
}
我假设您的 UITableView 具有全屏尺寸。如果不是,则需要根据需要调整 contentInset 大小。