为UITextField或UITextView显示键盘时调整界面

时间:2008-12-31 10:10:03

标签: cocoa-touch

我有一个表格,每个单元格都包含一个标签和一个文本字段。问题是,当我去编辑最后一行时,键盘隐藏了表格的下半部分,我无法看到正在键入的内容。如何将界面移动到键盘上方,以便查看正在键入的内容?

谢谢, 穆斯塔法

7 个答案:

答案 0 :(得分:10)

您需要为UIKeyboardDidShowNotificationUIKeyboardWillHideNotification事件注册viewController。当你得到这些,你应该调整你的表格的界限;键盘高度为170像素,因此只需适当缩小或增大表格边界,它应适当调整到键盘。

答案 1 :(得分:5)

此问题很复杂,具体取决于您的UI方案。在这里,我将讨论UITextField或UITextview驻留在UITableViewCell中的场景。

  1. 您需要使用NSNotificationCenter来检测UIKeyboardDidShowNotification事件。 见http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html。您需要缩小UITableView框架大小,使其仅占用键盘未覆盖的屏幕区域。

    1. 如果您点击UITableViewCell,操作系统将自动将单元格定位在UITableView的查看区域内。但是当你点击UITextView或UITableViewCell时它就不会发生,即使它位于UITableViewCell中。
  2. 您需要致电

    [myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 
    

    以编程方式“点击”单元格。

    如果同时实现两个点,您将在键盘正上方看到UITextView / Field位置。请记住,UITableView / Field所在的UITableViewCell不能高于“未覆盖”区域。如果您不是这种情况,则有不同的方法,但我不会在此讨论。

答案 2 :(得分:1)

答案 3 :(得分:0)

请确保您下方有足够的滚动空间。因为根据我的知识,iPhone会在键盘出现时自动调整并显示聚焦文本框。

答案 4 :(得分:0)

删除/注释正在修改矩形高度的行似乎可以解决问题。感谢。

修改后的代码:

# define kOFFSET_FOR_KEYBOARD 150.0     // keyboard is 150 pixels height

// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;

if (movedUp) {
    // If moving up, not only decrease the origin but increase the height so the view 
    // covers the entire screen behind the keyboard.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    // rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
    // If moving down, not only increase the origin but decrease the height.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    // rect.size.height -= kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;
[UIView commitAnimations];

}

答案 5 :(得分:0)

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasHidden:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
        keyboardVisible = NO;

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardVisible )
        return;

    if( activeTextField != MoneyCollected)
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-300;
        frame.size.height += keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardVisible = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) 
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-300;
        frame.size.height -= keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardVisible = NO;
}

答案 6 :(得分:0)

tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

试试这个我的编码,这对你有帮助