我在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;
}
答案 0 :(得分:0)
授予UITextView代表
CGRect activeField;
- (void)textViewDidBeginEditing:(UITextView *)textView
{
activeField = textView.frame;
}
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField.frame;
return YES;
}
尝试使用此代码。每次只给出textField rect size。当您调用文本视图时,您必须传递textview框架大小