键盘在ipad中隐藏不同方向的textfield

在我的iPad应用程序中,我有几个textViewtextField's 。 当我点击textField ,键盘覆盖了textField 。 所以我正在实现下面的代码来移动的TextView。 但在旋转portraitUpsideDown它不能正常工作。 它沿相反方向滑动屏幕。 那么我该如何解决这个问题?

 -(void) animateTextField: (UITextView *) textField up: (BOOL) up { int txtPosition = (textField.frame.origin.y - 540); const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed const float movementDuration = 0.3f; // tweak as needed int movement = (up ? -movementDistance : movementDistance); [UIView beginAnimations: @"anim" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.view.frame, 0, movement); [UIView commitAnimations]; } -(void)textViewDidBeginEditing:(UITextView *)textField { [self animateTextField: textField up: YES]; } -(void)textViewDidEndEditing:(UITextView *)textField { [self animateTextField: textField up: NO]; } -(BOOL)textFieldShouldReturn:(UITextView *)theTextField { [theTextField resignFirstResponder]; return YES; } 

这个解决scheme适用于iPhone,但是它考虑了两个方向。

你可以适应一下和瞧瞧:

http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html

疯,

只需添加另一个function:

 - (void) animateTextView: (UITextView*) textView up: (BOOL) up { const int movementDistance = 80; // tweak as needed const float movementDuration = 0.3f; // tweak as needed int movement = (up ? -movementDistance : movementDistance); [UIView beginAnimations: @"anim" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.view.frame, 0, movement); [UIView commitAnimations]; } 

然后像这样称呼它:

 - (void)textViewDidBeginEditing:(UITextView *)textView { [self animateTextView: textView up: YES]; } - (void)textViewDidEndEditing:(UITextView *)textView { [self animateTextView: textView up: NO]; } 

如果你的方法是这样的。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } 

尝试这个。 我不完全知道。 但是我想帮助你。 可能是xy坐标不能在任何方向上改变。 所以试试这个。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation=UIInterfaceOrienationPotraitUpsideDown){ //Declare txtPos globally... txtPos=(textField.frame.origin.y + 540); } if(interfaceOrientation=UIInterfaceOrienationPotrait) { txtPos=(textField.frame.origin.y - 540); } return(YES); } 

在有生命的方法。 将textPos分配给txtPositionvariables..

你应该使用键盘将显示并将隐藏通知捕获键盘事件,并相应地调整你的看法。

 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame); CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; UIViewAnimationOptions animationOption = animationCurve << 16; [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{ // adjust height using keyboardHeight } completion:^(BOOL finished) { }]; } - (void)keyboardWillHide:(NSNotification *)notification { CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame); CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; UIViewAnimationOptions animationOption = animationCurve << 16; [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{ // adjust height using keyboardHeight } completion:^(BOOL finished) { }]; } 

这篇博文详细解释了这一点

http://charlie.cu.cc/2015/10/solution-to-the-ios-software-keyboard-cover-part-of-the-ui/