当键盘覆盖文本字段时,我使用代码使屏幕滚动。
我有一个文本字段列表,我开始将每个文本域放在自己的uiview中以用于样式和组织目的。
现在我的代码只有在我开始实际输入文本字段时才有效。这是我从苹果复制的怪物。注意:我添加了一个参数 registerForKeyboardNotifications,以便我可以将其包含在父类中并从任何地方调用
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications:(UIScrollView *)scroll
{
_scroller = scroll;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSLog(@"hey");
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scroller.contentInset = contentInsets;
_scroller.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _activeField.frame.origin.y-kbSize.height);
[_scroller setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scroller.contentInset = contentInsets;
_scroller.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
_activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_activeField = nil;
}
我已将问题缩小到viewcontroller中的这一部分
for (id subView in _tpnScroll.subviews)
{
if ([subView isKindOfClass:[UIView class]]) {
UIView *thisView = subView;
for(id textfield in thisView.subviews){
if([textfield isKindOfClass:[UITextField class]]){
[textfield setDelegate:self];
}
}
}
}
原始代码是
for (id subView in _tpnScroll.subviews){
if ([subView isKindOfClass:[UITextField class]]) {
[subView setDelegate:self];
}
}
第一个例子中的。我循环遍历我的UIViews,找到UITextfields,并将委托设置为self(这部分我不完全理解,仍然试图理解委托的概念)
在第二个例子中,文本字段直接位于scrollview上,所以我只是直接遍历它们。
抱歉,如果这很罗嗦。
答案 0 :(得分:0)
尝试用此替换keyboardWasShown。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSLog(@"hey");
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scroller.contentInset = contentInsets;
_scroller.scrollIndicatorInsets = contentInsets;
// the scroll view will only scroll if the text field is not fully visible
// no need to check if it is actually covered
[_scroller scrollRectToVisible:[_scroller convertRect:_activeField.bounds fromView:_activeField]
animated:YES];
}
将文本字段的边界转换为滚动视图的坐标系,确保文本字段位于滚动视图内,即使它不在其中,也可以正常工作。