2012-10-12 59 views
1

我得到了一个UIViewController,我有一个UIScrollView里面有几个UITextFields。我从苹果那里得到了一段代码,似乎是从键盘下面移动内容。保持从键盘下面的内容

- (void)keyboardWasShown:(NSNotification *)notification 
{ 
    // Step 1: Get the size of the keyboard. 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height. 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
    // Step 3: Scroll the target text field into view. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= keyboardSize.height; 
    if (!CGRectContainsPoint(aRect, self.firstResponder.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10)); 
     [self.scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

我的UIScrollView是320x416,因为我有一个导航栏。此外,我的键盘获得44个像素,因为我正在添加一个UIToolBar,所以代码并不真正起作用。我尝试了各种配置来解决两个问题:

  1. 最后两个字段被UIToolBar +键盘覆盖,但只有最后一个字段触发UIScrollView移动。
  2. 除了UIScrollView在移动,由于UIToolBar,UITextField仍然在键盘后面。

UPDATE:这工作,但我敢肯定是错误的,如果是正确的,我不知道为什么,没有对我来说很有意义。有人可以修复/解释它吗?

- (void)keyboardWasShown:(NSNotification *)notification 
{ 
    // Step 1: Get the size of the keyboard. 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height. 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height + 44.0f, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
    // Step 3: Scroll the target text field into view. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= keyboardSize.height + 44.0f; 
    if (!CGRectContainsPoint(aRect, self.firstResponder.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10.0f - 44.0f - 44.0f - 44.0f)); 
     [self.scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

UPDATE1:还有一个错误,我在UIToolBar先前的按钮,如果我点击最后一个文本框,滚动视图上升,如果我搬回到第一文本视图,它在视图之外,因为滚动视图不会滚动。

回答

0

尝试了很多解决苹果的解决方案我结束了这个问题写一些代码自己后在描绘苹果代码背后的一般想法之后。顺便说一下,我认为苹果代码会使问题复杂化,并且不会真正起作用。

- (void)textFieldDidBeginEditing:(UITextField*)textField { 

    self.firstResponder = textField; 

    if (self.firstResponder.frame.origin.y + self.firstResponder.frame.size.height > self.view.bounds.size.height - (216 + 44)) { 

     double fix = (self.firstResponder.frame.origin.y + self.firstResponder.frame.size.height) - (self.view.bounds.size.height - (216 + 44)) + 10; 

     CGRect rect = CGRectMake(0, -fix, self.view.frame.size.width, self.view.frame.size.height); 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
     self.view.frame = rect; 
     [UIView commitAnimations]; 

    } else if (self.firstResponder.frame.origin.y + 10 < -self.view.frame.origin.y) { 

     double fix = -self.view.frame.origin.y - (-self.view.frame.origin.y - self.firstResponder.frame.origin.y) - 10; 

     CGRect rect = CGRectMake(0, -fix, self.view.frame.size.width, self.view.frame.size.height); 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
     self.view.frame = rect; 
     [UIView commitAnimations]; 

    } 

} 
1

尝试添加工具栏的高度以及测算,

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height + 44.0f, 0.0); 

而且使,

aRect.size.height = aRect.size.height - keyboardSize.height - 44.0f; 
+0

第一行解决1.但第二个使得UIScrollView下降而不是上升。 – lolol

+0

忘记了添加。改变这一点,CGPoint scrollPoint = CGPointMake(0.0,self.firstResponder.frame.origin.y - (keyboardSize.height-10 + 44.0f));如果仍然无法使用,请使用aRect.size.height = aRect.size.height - keyboardSize.height - 44.0f;部分。 – iDev

+0

这两种可能性都有问题,滚动视图正在关闭。男人,我尝试了很多东西。 – lolol

1

我都试过,你有同样的方法在编辑区域,但做了细微的变化的,如果条件:

!CGRectContainsRect(aRect, self.firstResponder.frame) 

这可以帮助你解决问题,即只有原点是键盘上方,但是文本区域被覆盖。