当键盘出现时,UITableView不会滚动

时间:2013-03-09 21:36:28

标签: uitableview ios6 keyboard uitextfield

我在这里尝试了Apple的例子:

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

然而,它似乎没有用,所以我显然遗漏了一些东西,只是无法弄清楚是什么。

我有一个ViewController.h包含:

@interface PreferencesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, defaultLocationChoice, UITextFieldDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSString *defaultLocation;
@property (nonatomic, retain) NSString *defaultTestType;
@property (nonatomic, assign) id <defaultLocationChoice> locationDelegate;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) IBOutlet UITextField *locationnameTextField;
@property (nonatomic, strong) IBOutlet UITextField *locationaddressTextField;
@property (strong, nonatomic) IBOutlet UITableView *scrollView;
@property (nonatomic, strong) IBOutlet UITextField *activeField;

.m文件中有所有Synthesized。

我在ViewController.m

中有Apple代码
- (void)viewDidLoad
{
    NSUserDefaults *sharedPref = [NSUserDefaults standardUserDefaults];
    defaultLocation =[sharedPref stringForKey:@"defaultLocation"];
    defaultTestType =[sharedPref stringForKey:@"defaultTestType"];

    [self registerForKeyboardNotifications];


    self.navigationItem.title = @"Preferences";

    [super viewDidLoad];

}

- (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;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField

{
    activeField = textField;
}



- (void)textFieldDidEndEditing:(UITextField *)textField

{
    activeField = nil;
}

包含TextFields的单元格在CellForRowAtIndexPath中处理如下(完整代码未显示):

  case 2:
            switch (indexPath.row) {
                case 0:

                    prefCell.textLabel.text = @"";
                    prefCell.accessoryType = UITableViewCellAccessoryNone;
                    prefCell.highlighted = NO;
                    locationnameTextField.frame = CGRectMake(5, 12, 300, 30);
                    locationnameTextField.font = [UIFont systemFontOfSize:13.0f];
                    locationnameTextField.placeholder = @"Enter location name i.e. New York, NY";
                    locationnameTextField.delegate = self;
                    locationnameTextField.returnKeyType = UIReturnKeyDone;
                    [prefCell.contentView addSubview:locationnameTextField];

                    break;
                case 1:
                    prefCell.textLabel.text = @"";
                    prefCell.accessoryType = UITableViewCellAccessoryNone;

                    locationaddressTextField.frame = CGRectMake(5, 12, 300, 30);
                     locationaddressTextField.font = [UIFont systemFontOfSize:13.0f];
                    locationaddressTextField.placeholder = @"Enter location address i.e. mcs.newyork.com";
                    locationaddressTextField.clearsOnBeginEditing = YES;
                    locationaddressTextField.delegate = self;
                    locationaddressTextField.returnKeyType = UIReturnKeyDone;

                    [prefCell.contentView addSubview:locationaddressTextField];

                    break;
            }

在运行应用程序时,键盘弹出,没有任何反应。

Apples示例适用于View,因此我上面更改的唯一代码是self.view.frameself.tableView.frame无效。

我添加了ScrollView作为添加,因为我没有。我的其他带有嵌入式TableView的ViewControllers不需要ScrollView来滚动。

层次结构如下:

enter image description here

任何帮助都会很棒,谢谢

3 个答案:

答案 0 :(得分:1)

您必须执行以下操作:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
 [yourTextField resignFirstResponder];
}

答案 1 :(得分:0)

当我注意到textfield的超视图时,我正在摸着同样的问题。如果我是对的,你在tableview的单元格中有文本字段。

现在,在您的keyboardWasShown:方法中,您正在检查活动文本字段是否隐藏在键盘下面,如下所示:

if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
} 

在您的情况下,if条件永远不会成立。由于活动文本字段的超级视图是一个单元格,activeField.frame.origin将返回一个引用UITableViewCell的点,而不是引用您的UIView。

因此,您需要做的是检查键盘下是否隐藏包含活动文本字段的单元格,如下所示:

CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, cellForActiveField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, cellForActiveField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
} 

要查找包含有效文本字段的单元格,您需要在tag方法中设置textfield的tableView:cellForIndexPath:属性,然后在textFieldDidBeginEditing:方法中检查标记。

有关详细信息,请参阅this question

希望这有帮助。

答案 2 :(得分:0)

自从我找到它之后,我使用了TPKeyboardAvoiding

效果很好,设置非常简单:

  1. 将UIScrollView添加到视图控制器的xib
  2. 将滚动视图的类设置为TPKeyboardAvoidingScrollView(仍在xib中,通过 身份检查员)
  3. 将所有控件放在该滚动视图中