0

私は、uitextfieldsとuitextviewsを持つipadでかなり長いフォームを持っています。キーボード通知を使用してキーボードが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のrectサイズのみを指定するたびに。テキストビューを呼び出すときは、textviewフレームサイズを渡す必要があります

于 2012-07-17T09:51:10.187 に答える