在我的應用程序中,我使用的UIScrollView
有UITextField
和UITextView
,我做了UITextField中鍵盤方向的編碼。它工作正常,但它不適用於UITextView
。在編輯UITextView
時,鍵盤完全隱藏了UITextView
。這裏是我的代碼在UITextView中的UIScrollView的鍵盤方向
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator/denominator;
if (heightFraction < 0.0) {
heightFraction = 0.0;
} else if (heightFraction > 1.0) {
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown) {
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
} else {
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
任何人都可以幫我清楚這一點嗎?
也在這些委託方法中定義了.h文件中的UITextFieldDelegate –
' - (void)textViewDidBeginEditing:(UITextView *)textView; '和' - (void)textViewDidEndEditing:(UITextView *)textView; ' 我應該怎麼辦 – iosdev
你在文本框使用的所有代碼使用的代碼,這些方法只是使用關鍵字「的TextView」而不是「文本框」 –