【发布时间】:2012-07-16 04:53:34
【问题描述】:
我在 ipad 中有很长的表格,有 uitextfields 和 uitextviews。当键盘使用键盘通知隐藏 uitextfield 时,我可以向上滚动。但是当 uitextview 变为活动状态并且它位于键盘下方时,它只会滑动到足以让我看到闪烁的光标。这是正常行为吗?如果不是,我如何向上滚动整个 uitextview,以便在编辑时可以看到整个 uitextview?
这是代码..
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark Keyboard Events
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-(void)keyboardWasShown:(NSNotification *)aNotification
{
if (displayKeyboard==YES) {
return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
offset = _scrollView.contentOffset;
CGRect viewFrame = _scrollView.frame;
viewFrame.size.height -= keyboardSize.height-tabbarHt;
_scrollView.frame = viewFrame;
CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;
}
-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{
if (!displayKeyboard) {
return;
}
_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}
【问题讨论】:
-
你必须在 UITextViewDelegate 方法中设置向上滚动的方法
-
@CocoaMatters 你能告诉我该怎么做吗?
-
UITextField 做得怎么样?
-
@CocoaMatters 使用键盘 didshow 和 didhide 通知..并在这些通知上调用相应的方法,并在这些方法中调整滚动视图大小,如果我的文本字段被隐藏,我向上滚动它。相同的代码部分适用于 textview。它向上滚动但不完全。只有一行 textview 是可见的。
-
你能贴出这段代码吗?
标签: ipad uitextview ios5.1