ios - 键盘在StoryBoard中隐藏UITableView

标签 ios iphone objective-c uitableview storyboard

我正在开发一个iPhone应用程序。我的UITableView上有一个简单的ViewController。在UITableView中,我在单元格中添加了UITextField。我的问题是,当我单击特定单元格中的UITextField时,键盘将隐藏整个UITableView。我已经搜索过,但是找不到成功的解决方案。我正在添加UITextField像这样:

UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(250, 15, 60, 30)];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.backgroundColor = [UIColor clearColor];
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
textField.tag =TEXT_VIEW_TAG;
[cell.contentView addSubview:textField];

如果有人知道其解决方案,请告诉我。

最佳答案

实际上,添加UITableViewController可以自己处理。但是,如果确实需要使用UITableView,但仍希望键盘不隐藏UITableView,则在打开键盘时需要向上移动UITableView。为此,您可以更改UITextFieldBeginEditing方法中的y位置,也可以使用UIKeyboard通知。您需要在viewDidLoad方法中添加以下代码行:

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

然后,您可以添加以下方法:
    - (void) myKeyboardWillHideHandler:(NSNotification *)notification {

            [UIView animateWithDuration:0.2 animations:^{
                YOUR_TBL_VIEW.frame = CGRectMake(YOUR_TBL_VIEW.frame.origin.x, YOUR_TBL_VIEW.frame.origin.y + 80, YOUR_TBL_VIEW.frame.size.width, YOUR_TBL_VIEW.frame.size.height);
            }];
        }

    - (void) myKeyboardWillShowHandler:(NSNotification *)notification {
            [UIView animateWithDuration:0.2 animations:^{
                YOUR_TBL_VIEW.frame = CGRectMake(YOUR_TBL_VIEW.frame.origin.x, YOUR_TBL_VIEW.frame.origin.y - 80, YOUR_TBL_VIEW.frame.size.width, YOUR_TBL_VIEW.frame.size.height);
            }];


}

关于ios - 键盘在StoryBoard中隐藏UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21906081/

相关文章:

ios - UIScrollView内的UILabel上的触摸检测

ios - 调用具有关键字 "getter=isXXX"的 Objective-c 属性

iOS - reloadData 时未调用 cellForRowAtIndexPath

ios - TabBarController 的界面方向

iphone - 如何在 iOS 中制作其他语言的键盘

iphone - UITableView 函数 visibleCells 是否返回自定义单元格数据?

ios - Restkit - 无法使用 RKDotNetDateFormatter 设置首选输出日期格式化程序

iphone - 通过保持纵横比和宽度调整 UIImage 的大小

iphone - 通过 AppStore 更新后如何测试对钥匙串(keychain)属性的访问?

ios - 在 iPad Pro 中没有调用 shouldStartLoadwithRequest