【问题标题】:How do I delay the iOS keyboard from popping up?如何延迟 iOS 键盘弹出?
【发布时间】:2013-02-28 15:19:13
【问题描述】:

所以...当键盘弹出时,我的 UIScrollView 向上移动,这很有效...除了它们 UIScrollView 和键盘不同步...首先,键盘弹出,然后UIScrollView。

我知道有一种方法可以延迟键盘,使其在视图向上滚动的同时显示;我怎么做??我在 viewDidLoad 中试过这个:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

...我也有这个:

- (void)keyboardWillShow:(NSNotification *)notification
{
    [NSTimer scheduledTimerWithTimeInterval:4000 target:self selector:@selector(keyboardWillShow:) userInfo:nil repeats:NO];
}

(我知道,4000 是一个巨大的数字,但我想确保有延迟!!)

另外,当我关闭键盘而不是平滑滚动时,UIScrollView 只是简单地跳回原位而不是缓和下来......有没有合理的方法来解决这个问题?


更新:

知道了...感谢 Steven Fisher 帮助我走上了正确的道路...我将所有内容移至keyboardWillShow,并添加了以下代码:

[UIScrollView beginAnimations:nil context:NULL];
[UIScrollView setAnimationDelegate:self];
[UIScrollView setAnimationDuration:.32];
[UIScrollView setAnimationBeginsFromCurrentState:NO];

不知何故,当键盘消失时,这也解决了我的“跳跃”问题!哇哦!

【问题讨论】:

  • 你的工作倒退了。您应该计时滚动视图偏移更改为键盘动画。而且很难说为什么它“跳回来”而不看一些相关代码。
  • 好的,那我该怎么做呢?

标签: ios uiscrollview keyboard


【解决方案1】:

这是苹果文档针对这种情况给出的代码。

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
   [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWasShown:)
        name:UIKeyboardDidShowNotification object:nil];

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

}



// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
 {
   NSDictionary* info = [aNotification userInfo];
   CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
   // Your application might not need or want this behavior.
   CGRect aRect = self.view.frame;
   aRect.size.height -= kbSize.height;
   if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
   }
  }



// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{  
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}

【讨论】:

  • 这正是我使用的代码,但同样,键盘和滚动视图不会同时上升:首先是键盘,然后是滚动视图。如何让它们同时出现?
  • 挂钩到UIKeyboardWillShowNotification,而不是UIKeyboardDidShowNotification。
  • 另外:这不是问题的一部分,但如果你想躲避键盘并支持旋转,你应该确保永远不要从 viewWillAppear 显示它,因为视图坐标没有被设置。相反,在viewWillAppear 中使用dispatch_async(dispatch_get_mainqueue(),^{[field becomeFirstResponder];});。这将确保坐标已针对当前方向进行了更新,但看起来仍然是无缝的。 (如果您不让设备旋转,请不要担心。)
【解决方案2】:
  - (void)viewDidLoad
  {

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

 - (void)keyboardWasShown:(NSNotification *)notification
{
  // To avoid keyboard hides the view
  CGRect frame = self.view.bounds; 
if (capitalTextField.enabled ==YES) 
{
     if ([notification name]== UIKeyboardDidShowNotification ) 
     {

        frame.origin.y += 200;
        [self.scrollView scrollRectToVisible:frame animated:YES];
     }
     else
     {
        frame.origin.y -= 200;
        [self.scrollView scrollRectToVisible:frame animated:YES]; 
     }
    }
  }

【讨论】:

    【解决方案3】:

    下面的代码会延迟弹出

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];
    [textField becomefirstresponder];
    [UIView commitAnimations];
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 1970-01-01
      • 2017-12-24
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2013-01-13
      • 2014-08-21
      相关资源
      最近更新 更多