2013-08-07 37 views
1

我有一个包含文本字段和文本视图的UIScrollView。当键盘出现时,我有代码将文本字段向上移动,因此键盘不覆盖文本字段。此代码工作在纵向视图很大:从横向切换到纵向后,将UIScrollView保留在正确的位置

-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    if(textField) { 
     [textField resignFirstResponder]; 
    } 
    return NO; 
} 

-(void)textFieldDidBeginEditing:(UITextField *)textField { 
    if (textField == self.nameField) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.3]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height); 
     [UIView commitAnimations]; 
    } 
} 

-(void)textFieldDidEndEditing:(UITextField *)textField { 
    if (textField == self.nameField) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.3]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height); 
     [UIView commitAnimations]; 
    } 
} 

-(void)textViewDidBeginEditing:(UITextView *)textField { 
    if (textField == self.questionField) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.3]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height); 
     [UIView commitAnimations]; 
    } 
} 

-(void)textViewShouldEndEditing:(UITextView *)textField { 
    if (textField == self.questionField) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.3]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation; 

     self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height); 
     [UIView commitAnimations]; 
    } 
} 

当我旋转iPhone模拟器到横向视图,这个(不奇怪)不起作用。我怎样才能让文本字段向上移动,以在横向和纵向视图中输入时看到它?

此外,如果我在显示键盘时从风景旋转到纵向,关闭键盘后,滚动视图向下移动而不是排列在原始位置。我怎样才能避免这种情况?

回答

2

Apple的文档Managing the Keyboard显示了在标题为“移动位于键盘下的内容”标题下的正确方法。

您基本上将您的内容放在滚动视图上,并使用UIKeyboardDidShowNotificationUIKeyboardWillHideNotification通知来调整内容偏移量。

但是,代码有些不完整。键盘大小计算在横向模式下不起作用。为了解决这个问题,将以下:

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

有了这个:

// Works in both portrait and landscape mode 
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
kbRect = [self.view convertRect:kbRect toView:nil]; 

CGSize kbSize = kbRect.size; 
+0

感谢您的帮助!但是,我在我的应用程序中有textViews和textFields。这适用于textView,但不适用于textField。任何想法,为什么这是? – user2661683

相关问题