2011-05-11 78 views
4

在我的应用程序中,当我单击文本字段时,键盘隐藏它。请帮助我 - 当我点击文本字段时,如何移动视图。我在textFieldDidBeginEditing:使用此代码移动视图,以便键盘不隐藏文本字段

self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0); 
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0); 

但它不起作用。

+0

的可能重复[如何使一个的UITextField向上移动时,键盘的存在(http://stackoverflow.com/questions/1126726/how-to -make-a-uitextfield-move-up-when-keyboard-is-present) – 2011-05-13 05:54:20

回答

1

你可以做到以下几点,但首先要确保你已经设置的UITextField代表你的自我和

#define kOFFSET_FOR_KEYBOARD 350; 

在顶部。这是你想要多远视图要移位

//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 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    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. 

     if (rect.origin.y == 0) { 
      rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
      //rect.size.height += kOFFSET_FOR_KEYBOARD; 
     } 

    } 
    else 
    { 
     if (stayup == NO) { 
      rect.origin.y += kOFFSET_FOR_KEYBOARD; 
      //rect.size.height -= kOFFSET_FOR_KEYBOARD; 
     } 
    } 
    self.view.frame = rect; 
    [UIView commitAnimations]; 
} 


- (void)keyboardWillHide:(NSNotification *)notif { 
    [self setViewMovedUp:NO]; 
} 


- (void)keyboardWillShow:(NSNotification *)notif{ 
    [self setViewMovedUp:YES]; 
} 


- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    stayup = YES; 
    [self setViewMovedUp:YES]; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField { 
    stayup = NO; 
    [self setViewMovedUp:NO]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification object:self.view.window]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification object:self.view.window]; 
} 

- (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]; 
} 
+4

这个例子有几个缺点,基于一些错误的假设;从来没有物理键盘,只有肖像模式,苹果永远不会改变键盘的大小,或动画的长度。如果用户希望例如在不先关闭键盘的情况下在文本字段之间进行切换,则滚动到被偏移的内容也是不可能的。 – PeyloW 2011-05-11 14:24:59

+0

thanx但我不想移动我的视图为所有文本框,所以请告诉我如何选择特定的文本框 – sandy 2011-05-11 14:30:22

+0

你好,我没有得到如何防止我的观点上移键盘不隐藏我的文本框。请帮助我 – sandy 2011-05-12 11:10:16

13

你不应该相信textFieldDidBeginEditing:调整为键盘,因为这种方法将被调用,即使用户使用物理键盘,其中屏幕键盘就会打字不被显示。

取而代之的是听取UIKeyboardWillShowNotification,这只有在键盘实际显示时才会触发。您需要执行三个步骤:

  1. 从通知userInfo字典中确定键盘的实际大小。尺寸将不同于风景/肖像以及不同的设备。
  2. 使用确定的尺寸更新contentInset。你可以做动画,通知甚至会告诉你键盘动画的持续时间。
  3. 将文本框滚动到视图中,很容易忘记!

你会发现更多的信息和示例代码here

+0

请告诉我代码知道在景观和potraite键盘的大小 – sandy 2011-05-11 14:41:44

+0

@Sandy:完整的代码,与工作示例可以在我链接到答案的developer.apple.com上的链接上找到。 – PeyloW 2011-05-11 14:46:03

+0

请记住,Apple示例忘记使用文本框的高度。导致键盘边界上的字段未显示或未完全显示。 – ophychius 2012-07-16 11:25:23