0

所以......当键盘弹出时,我让我的 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];

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

4

3 回答 3

0
  - (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]; 
     }
    }
  }
于 2013-02-28T03:50:01.767 回答
0

下面的代码会延迟弹出

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[textField becomefirstresponder];
[UIView commitAnimations];
于 2013-02-28T07:19:13.573 回答
0

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

// 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;
}
于 2013-02-28T03:37:02.333 回答