0

我在 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;
}
4

1 回答 1

0

给你的 UITextView 委托

CGRect activeField;

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    activeField = textView.frame;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
   activeField = textField.frame;
   return YES;
}

尝试使用此代码。每次您只给出 textField 矩形大小。当您调用文本视图时,您必须传递 textview 框架大小

于 2012-07-17T09:51:10.187 回答