2011-11-21 68 views
1

打开键盘时,我有一个UIViewController包含自定义细胞UITableView,在细胞内是UILabels,一对夫妇的不可编辑UITextView和一个可编辑UITextView。现在,当我点击靠近桌子底部或底部的UITextView之一时,UITextView被键盘覆盖。我试过http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,它对textfield/textview非常有用,但不能用自定义单元格工作。任何帮助或建议如何去做这件事?避免视图从一个UITextView一个UITableView内与自定义单元格

回答

0

两个解决方案:

首选:使用一个UITableViewController,而不是一个UIViewController作为一个会自动确保您的键盘不会隐藏编辑字段。

哈克How to make a UITextField move up when keyboard is present?

+0

好,我不能使用的UITableViewController因为我得到了很多的东西,这个视图中,而不是仅仅的UITableView。我会尝试另一个。我稍后会回到这里。 – Diffy

0

一个简单的解决方案。实现heightForFooter方法,并让它返回(比如说)100的值,并且当您选择UITableView中的单元格时,它们将仅向上滑动该高度,并且键盘不会覆盖该视图。

0

我一直使用双折叠解决方案。

  1. 调整表格的大小,使其适合较小的区域。
  2. 滚动到我们希望看到的单元格。 (我们需要为此重新调整表格大小,否则您仍然无法到达表格中最后一对单元格。)

为此,我注册了键盘显示/隐藏事件并在被叫时采取相应行动。

- (void)keyboardWillShow:(NSNotification *)note { 
    [self updateForKeyboardShowHide:note appearing:YES]; 
} 
- (void)keyboardWillHide:(NSNotification *)note { 
    [self updateForKeyboardShowHide:note appearing:NO]; 
} 

- (void)updateForKeyboardShowHide:(NSNotification *)note appearing:(BOOL)isAppearing { 
    // ignore notifications if our view isn't attached to the window 
    if (self.view.window == nil) 
     return; 

    CGFloat directionalModifier = isAppearing?-1:1; 
    CGRect keyboardBounds = [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
    CGFloat animationDuration = [[note.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; 

    // figure out table re-size based on keyboard 
    CGFloat keyboardHeight; 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (UIInterfaceOrientationIsPortrait(orientation)) 
     keyboardHeight = keyboardBounds.size.height; 
    else 
     keyboardHeight = keyboardBounds.size.width; 

    [UIView animateWithDuration:animationDuration animations:^{ 
     // resize table 
     CGRect newFrame = table.frame; 
     newFrame.size.height += [self calculateKeyboardOffsetWithHeight:keyboardHeight] * directionalModifier; 
     table.frame = newFrame;   
    } completion:^(BOOL finished){ 
     // scroll to selected cell 
     if (isAppearing) { 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textFieldInEdit.tag inSection:0]; 
      [table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
     } 
    }]; 
} 

- (CGFloat)calulateKeyboardOffsetWithHeight:(CGFloat)keyboardHeight { 
    // This depends on the size and position of your table. 
    // If your table happen to go all the way to the bottom of 
    // the screen, you'll needs to adjust it's size by the whole keyboard height. 
    // You might as well ditch this method and inline the value. 
    return keyboardHeight; 

    // My table did not go to the bottom of the screen and the position was 
    // change dynamically so and there was long boring calculation I needed to 
    // do to figure out how much my table needed to shrink/grow. 
} 
+0

你忘了包含你的方法“calculateKeyboardOffsetWithHeight:”,没有它,这将不会工作 – Adam

+0

@Adam我不认为这种方法是必要的,但我可以看到它是如何引起混淆,因此我添加并解释它。我希望有所帮助。 – DBD

0

当你的表视图包含像UITextFieldUITextView和表视图足够长以覆盖屏幕数据输入字段,你将有由键盘隐藏着一个问题访问数据输入字段。
为了克服这个问题,有两种解决方案:

  1. 最简单和推荐的方法是使用UITableViewController代替UIViewController,其自动确保键盘不会隐藏编辑字段(如果可能使用这种方法避免UI调整不便

  2. 如果您使用UIViewControllerUITableView作为其子视图。您可以通过观察UIKeyboardWillShowNotificationUIKeyboardWillHideNotification

    - (void)registerForKeyboardNotifications 
    { 
         [[NSNotificationCenter defaultCenter] addObserver:self 
                   selector:@selector(keyboardWillShow:) 
                    name:UIKeyboardWillShowNotification object:nil]; //Posted immediately prior to the display of the keyboard 
    
         [[NSNotificationCenter defaultCenter] addObserver:self 
                   selector:@selector(keyboardWillHide:) 
                    name:UIKeyboardWillHideNotification object:nil]; //Posted immediately prior to the dismissal of the keyboard. 
    } 
    
    
    - (void)keyboardWillShow:(NSNotification *)aNotification 
    { 
        CGRect keyboardBounds = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
    
        [UIView beginAnimations:nil context:nil]; 
        [UIView setAnimationBeginsFromCurrentState:YES]; 
    
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); //when keyboard is up, that time just bring your text filed above the keyboard 
        self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); 
    
        [self.tableView scrollToRowAtIndexPath:[self findIndexPathToScroll] 
              atScrollPosition:UITableViewScrollPositionTop 
                animated:YES]; //findIndexPathToScroll implementation not shown 
        [UIView commitAnimations]; 
    } 
    
    - (void)keyboardWillHide:(NSNotification *)aNotification 
    { 
        [UIView beginAnimations:nil context:nil]; 
        [UIView setAnimationBeginsFromCurrentState:YES]; 
        self.tableView.contentInset = UIEdgeInsetsZero; //Once keyboard is hidden then bring back your table into your original position. 
        self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero; 
        [UIView commitAnimations]; 
    } 
    
    • registerForKeyboardNotifications滚动你的用户界面的框架 - 调用此方法时您加载的UITableView,即:viewDidLoad中

    • findIndexPathToScroll - (未显示试行),是你的业务逻辑准备索引路径表视图应滚动

    • removeObserver'UIKeyboardWillShowNotification'和'UIKeyboardWillHideNotif ication”无论是在deallocviewDidUnload
1
I fixed the issue. Please see my solution below: 

1. First declare a global varibale called "activeFileld" 
@property(nonatomic,strong)id activeFiled; 

2. Create a method called "registerForKeyboardNotifications" 
- (void)registerForKeyboardNotifications 
{ 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(keyboardWillShow:) 
                name:UIKeyboardWillShowNotification object:nil]; //Posted immediately prior to the display of the keyboard 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(keyboardWillHide:) 
                name:UIKeyboardWillHideNotification object:nil]; //Posted immediately prior to the dismissal of the keyboard. 
} 

3. Called the above method in viewWillAppear: 

-(void)viewWillAppear:(BOOL)animated{ 

    [super viewWillAppear:animated]; 
    //Register kryboard Notification 
    [self registerForKeyboardNotifications]; 
} 
4. Call the Delegate method for UitextFieldd Or UitextView 

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

     self.activeField = sender; 
} 
- (void)textFieldDidEndEditing:(UITextField *)sender{ 
     self.activeField = nil; 
} 

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    // save the text view that is being edited 
    _notes = textView.text; 

} 
- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    // release the selected text view as we don't need it anymore 
    _activeField = nil; 
} 

5. 

- (void)keyboardWillShow:(NSNotification *)notification 
{ 

    if([_activeField isKindOfClass:[UITextField class]]) { 

     NSDictionary* info = [notification userInfo]; 
     NSLog(@"Dictionary %@",info); 
     CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
     kbRect = [self.view convertRect:kbRect fromView:nil]; 

     UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0); 
     self.tableView.contentInset = contentInsets; 
     self.tableView.scrollIndicatorInsets = contentInsets; 

     CGRect aRect = self.view.frame; 
     aRect.size.height -= kbRect.size.height; 

     UITextField *textField = (UITextField*)_activeField; 
     if (!CGRectContainsPoint(aRect, textField.frame.origin)) { 
      [self.tableView scrollRectToVisible:textField.frame animated:YES]; 
     } 
    }else if([_activeField isKindOfClass:[UITextView class]]) { 

     NSDictionary* info = [notification userInfo]; 
     NSLog(@"Dictionary %@",info); 
     CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
     kbRect = [self.view convertRect:kbRect fromView:nil]; 

     UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0); 
     self.tableView.contentInset = contentInsets; 
     self.tableView.scrollIndicatorInsets = contentInsets; 

     CGRect aRect = self.view.frame; 
     aRect.size.height += kbRect.size.height; 

     UITextView *activeTextView = (UITextView*)_activeField; 
     if (!CGRectContainsPoint(aRect, textField.superview.superview.frame.origin)) { 
      [self.tableView scrollRectToVisible:activeTextView.superview.superview.frame animated:YES]; 

     } 


    } 





} 

// Called when the UIKeyboardWillHideNotification is received 
- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.tableView.contentInset = contentInsets; 
    self.tableView.scrollIndicatorInsets = contentInsets; 
} 
相关问题