显示键盘时出现滚动 UITableViewController

Posted

技术标签:

【中文标题】显示键盘时出现滚动 UITableViewController【英文标题】:Issue scrolling UITableViewController when showing keyboard 【发布时间】:2016-01-26 03:53:03 【问题描述】:

我有一个 UITableViewController,其中一个自定义单元格包含一个 UITextView。当我点击文本视图时,键盘会按预期弹出,并且表格视图会滚动以使文本视图可见。到现在为止还挺好。问题是表格视图随后再次滚动,以使文本视图正好位于键盘顶部的下方。这是一个动画 gif,演示了从模拟器捕获的问题:

这曾经不是 UITableViewController,并且滚动是手动执行的以模仿 UITableViewController 的行为。我最近将其更改为嵌入在 UIContainerView 中的 UITableViewController(以容纳底部的两个按钮)并删除了所有手动滚动代码。该问题也发生在该更改之前(并且是其动机之一)。任何想法可能导致此问题?

【问题讨论】:

你弄明白了吗? 试试这个***.com/a/61875191/6314955 【参考方案1】:

检查一下希望这有效... ViewController.h

    @interface MyUIViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>

     UITableView *myTableView;
     UITextField *actifText;


@property (nonatomic, retain) IBOutlet UITableView *myTableView;
@property (nonatomic, retain) IBOutlet UITextField *actifText;

- (IBAction)textFieldDidBeginEditing:(UITextField *)textField;
- (IBAction)textFieldDidEndEditing:(UITextField *)textField;

-(void) keyboardWillHide:(NSNotification *)note;
-(void) keyboardWillShow:(NSNotification *)note;

@end

在 ViewController.m 中

    @implementation MyUIViewController

@synthesize myTableView;
@synthesize actifText;

- (void)viewDidLoad 

    // Register notification when the keyboard will be show
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(keyboardWillShow:)
                                          name:UIKeyboardWillShowNotification
                                          object:nil];

    // Register notification when the keyboard will be hide
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(keyboardWillHide:)
                                          name:UIKeyboardWillHideNotification
                                          object:nil];


// To be link with your TextField event "Editing Did Begin"
//  memoryze the current TextField
- (IBAction)textFieldDidBeginEditing:(UITextField *)textField

    self.actifText = textField;


// To be link with your TextField event "Editing Did End"
//  release current TextField
- (IBAction)textFieldDidEndEditing:(UITextField *)textField

    self.actifText = nil;


-(void) keyboardWillShow:(NSNotification *)note

    // Get the keyboard size
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

    // Detect orientation
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect frame = self.myTableView.frame;

    // Start animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];

    // Reduce size of the Table view 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
        frame.size.height -= keyboardBounds.size.height;
    else 
        frame.size.height -= keyboardBounds.size.width;

    // Apply new size of table view
    self.myTableView.frame = frame;

    // Scroll the table view to see the TextField just above the keyboard
    if (self.actifText)
      
        CGRect textFieldRect = [self.myTableView convertRect:self.actifText.bounds fromView:self.actifText];
        [self.myTableView scrollRectToVisible:textFieldRect animated:NO];
      

    [UIView commitAnimations];


-(void) keyboardWillHide:(NSNotification *)note

    // Get the keyboard size
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

    // Detect orientation
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect frame = self.myTableView.frame;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];

    // Increase size of the Table view 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
        frame.size.height += keyboardBounds.size.height;
    else 
        frame.size.height += keyboardBounds.size.width;

    // Apply new size of table view
    self.myTableView.frame = frame;

    [UIView commitAnimations];


@end
Swift 1.2+ version:
class ViewController: UIViewController, UITextFieldDelegate 
    @IBOutlet weak var activeText: UITextField!
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() 
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("keyboardWillShow:"),
            name: UIKeyboardWillShowNotification,
            object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("keyboardWillHide:"),
            name: UIKeyboardWillHideNotification,
            object: nil)
    

    func textFieldDidBeginEditing(textField: UITextField) 
        activeText = textField
    

    func textFieldDidEndEditing(textField: UITextField) 
        activeText = nil
    

    func keyboardWillShow(note: NSNotification) 
        if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() 
            var frame = tableView.frame
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationBeginsFromCurrentState(true)
            UIView.setAnimationDuration(0.3)
            frame.size.height -= keyboardSize.height
            tableView.frame = frame
            if activeText != nil 
                let rect = tableView.convertRect(activeText.bounds, fromView: activeText)
                tableView.scrollRectToVisible(rect, animated: false)
            
            UIView.commitAnimations()
        
    

    func keyboardWillHide(note: NSNotification) 
        if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() 
            var frame = tableView.frame
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationBeginsFromCurrentState(true)
            UIView.setAnimationDuration(0.3)
            frame.size.height += keyboardSize.height
            tableView.frame = frame
            UIView.commitAnimations()
        
    

【讨论】:

谢谢,但这不起作用,我已经尝试过与此非常相似的代码。

以上是关于显示键盘时出现滚动 UITableViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何在点击时出现键盘时管理UITextField的高度?

显示键盘时出现 React-native / Splash-Screen

使用嵌套适配器垂直滚动时出现滞后问题 | IGListKit

滚动网页时出现水波浪纹怎么办

在android列表视图中向下滚动时出现***Exception

集合视图单元格快速滚动到中心时出现问题