2013-03-19 47 views
-3

除了顶部文本字段,当我开始键入时,iphone键盘会隐藏所有剩余的文本字段。如何在键盘上方显示活动文本字段,然后在隐藏键盘时将其恢复正常?如何在iPhone键盘显示时显示我的活动文本框?

+0

请问你可以在这里粘贴代码吗? – 2013-03-19 07:19:53

+0

弹出虚拟键盘时向上移动整个视图。看到这个。 http://stackoverflow.com/questions/15145341/undock-keyboard-programmatically – 2013-03-19 07:32:57

回答

0

您必须在视图顶部使用滚动视图,并根据您选择的文本字段实施数学contentOffset设置。 您选择的文本框可以从委托方法获得和偏移的计算可以用文本框框架

1

手表UIKeyboardWillShowNotificationUIKeyboardWillHideNotification来完成。随这些通知提供的NSNotification对象将包含userInfo字典,其中包含有关键盘完成动画后帧所具有的信息。根据这些通知调整您的布局,以保持您想要被键盘覆盖的东西。

1

在将所有文本字段置于滚动视图之前,请使用以下代码。

#define kOFFSET_FOR_KEYBOARD 80.0 

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillHide) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 
+0

如果这个代码太高,不会隐藏textField吗? – Jsdodgers 2013-03-19 07:26:29

+0

该代码适用于文本字段,但不适用于位于底部的uitextview.Please help – zzzzz 2013-03-19 07:38:58

+0

是的,如果这在githhub中使用BSKeyboarcontrols – Vinodh 2013-03-19 08:55:18

1

把你UITextFieldUIScrollView和使用这样的UIScrollViewcontentOffSet属性:

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField == txt1) // 
    { 
     [txt1 becomesFirstResponder]; 
     scrMainView.contentOffset = CGPointMake(0, 20); 
    } 

    else if (textField == txt2) // 
    { 
     [txt1 resignFirstResponder]; 
     [txt2 becomesFirstResponder]; 
     scrMainView.contentOffset = CGPointMake(0, 60); 
    } 
    //And so on.. 
    return YES; 
} 

希望,你会得到答案。

谢谢。

+0

什么是scrMainView?提到适当的细节,否则用户不会知道你在暗示什么。 'scrMainView'可能是scollview的权利? – 2013-03-19 09:45:44

+0

是的。 scrMainView是我的滚动视图,所以它会将内容偏移量设置得稍微高一点,所以用户可以轻松地进行交互。 – 2013-03-19 09:47:03

+0

然后请提供那些细节。只需看看我的编辑。它会清楚地说明你的答案。 – 2013-03-19 09:48:39