键盘出现时,UIScrollbar不会向上滚动

时间:2013-07-04 22:19:55

标签: ios keyboard scrollbar

我有一个故事板,里面插入了一个滚动条,因为要显示的内容太多而无法一次性显示。 滚动条包含:

  • 图片视图
  • 标签
  • 分组表格视图

表视图的行有两种。第一种(由控制器regularRegistrationCellVC定义)包含文本字段和标签。 当我在任何生成的单元格中单击任何文本字段时,会出现键盘并隐藏文本字段。此外,无法向上滚动视图内容以查看键盘下方的内容。 因此,按照apple的指令,我将此代码添加到包含滚动条的控制器:

注册键盘通知

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

发送UIKeyboardDidShowNotification时调用

- (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);
    scrollPanel.contentInset = contentInsets;
    scrollPanel.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);
        [scrollPanel setContentOffset:scrollPoint animated:YES];
    }
}

键盘消失时调用

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollPanel.contentInset = contentInsets;
    scrollPanel.scrollIndicatorInsets = contentInsets;
}

管理开始编辑的事件

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
    NSLog(@"inizio digitazione\n");
}

管理结束编辑事件

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

其中 activeField 是我的控制器的全局变量。

然后,我修改了创建第一种单元格的代码部分,这样每个第一类新单元都将我的视图控制器作为委托:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"regularRegistrationCell";

        regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.regularFieldName.text = [self.orientationItems objectAtIndex:indexPath.row];
        cell.regularTextField.delegate = self;
        return cell;

    }

    else{
        static NSString *CellIdentifier = @"orientationRegistrationCell";

        orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.fieldLabel.text = [self.orientationItems objectAtIndex:[orientationItems count]-1];

        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
        NSString *val = nil;

        if (standardUserDefaults)
            val = [standardUserDefaults objectForKey:@"orientation"];

        if (val == nil) 
            cell.orientationLabel.text = @"Eterosessuale";

        else
            cell.orientationLabel.text = val;

        return cell;

    }
}

最后,我宣布我的视图控制器(包括滚动条及其内容)实现 的 UITextFieldDelegate 即可。 使用NSLog打印,我发现每次点击文本字段时,管理事件的功能肯定会被执行。

你能说出我在哪里错了吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您是否尝试过TPKeyboardAvoiding

在这个答案中修改几行。 Continue reading

相关问题