【问题标题】:Scroll UITableview with changing height and text fields to avoid keyboard使用更改高度和文本字段滚动 UITableview 以避免键盘
【发布时间】:2013-11-17 02:38:28
【问题描述】:

我正在尝试设置 tableview 以在选择隐藏在键盘后面的文本字段时滚动。我尝试了多种我发现的方法,包括使用键盘通知、textFieldDid/ShouldBegin/EndEditing 等,但似乎没有一个每次都有效。

这是我正在使用的屏幕截图:

我有两个问题:

首先,我使用日期选择器代替底部文本字段的键盘(屏幕截图中的屏幕外,但您可以理解)。由于这在技术上不是键盘,因此我用于设置键盘偏移的方法不适用于此文本字段。我确信我可以获取日期选择器的高度并相应地调整如果这是当前选择的项目,但我想知道是否有更简单的方法将其合并到键盘方法中。

其次,当添加更多单元格时,偏移量变得不正确。这个视图的设置方式是一个分成多个部分的表格视图。当用户点击“添加进一步支持”按钮时,它会在支持部分中插入一行。当我尝试设置表格滚动偏移时,似乎没有注册由于此而发生的高度变化。有没有办法让我正确注册高度?

这是一些相关的代码

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeField = textField;
    [self setOffsetForKeyboard];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.activeField == self.dateTextField) {
        [self datePickerValueChanged:nil];
    }
    self.activeField = nil;
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWillShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    self.keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self setOffsetForKeyboard];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self.myTable setContentOffset:CGPointMake(0.0, -(self.navigationController.navigationBar.frame.size.height + kStatusBarHeight))animated:YES];
}

- (void)setOffsetForKeyboard{
    CGPoint location =[self.activeField.superview convertPoint:self.activeField.frame.origin toView:nil];
    if (location.y > self.view.frame.size.height - self.keyboardSize.height-kKeyboardOffset) {
        [self.myTable setContentOffset:CGPointMake(0.0, location.y-self.keyboardSize.height-kKeyboardOffset) animated:YES];
    }
}

【问题讨论】:

    标签: ios iphone objective-c uitableview


    【解决方案1】:

    这是你的解决方案,你会喜欢的->

    https://github.com/michaeltyson/TPKeyboardAvoiding

    【讨论】:

    • 这看起来不错,但它有一些奇怪的行为。我最终只是将 uitableviewcontroller 子类化了
    • @Ian 同意。人们一直在围绕这些问题抛出那个库,但它确实有问题。
    • @MattQuiros-在 TPKeyboardAvoiding 类中添加您自己的代码...我已经在其中添加了自己的代码,并且它运行良好。所以我的朋友,你一定知道如何即兴创作 3rd 方库。
    【解决方案2】:

    所以我最终只是继承了内置此功能的 UITableViewController,而不是我之前使用的 UIViewController。像魅力一样工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      相关资源
      最近更新 更多