How do I stop a tableView from scrolling when the keyboard appears? [duplicate]

大兔子大兔子 提交于 2019-12-23 11:58:34

问题


I have a UITableViewController that each contains cells with a UITextView positioned at the top of each cell. Naturally when an interaction with the textBox begins, the keyboard will appear and at the same time the entire table will also scroll up along as the keyboard appears causing the textBox to go out of view.

Because I've enabled pagination to my tableView so after scrolling up it will scroll down again so that the textBox is in view.

I would like to know if it is possible to disable the table from scrolling when the keyboard appears and if yes how?


回答1:


The autoscroll-behavior is located in the UITableViewCONTROLLER functionality. To disable the automatic scrolling I found two ways:

1) use instead of the UITableViewController simply a UIViewController - set the datasource and delegate on your own

2) Override the viewWillAppear-Routine - and DON´T call [super viewWillAppear: animated] With both solution you disable not only the Autoscroll, but also some other nice but not

essential features, that are described in the overview of Apple´s class reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html




回答2:


we can disable the tableview scrolling in multiple ways.

1) In textView delegate methods

- (void)textViewDidBeginEditing:(UITextView *)textView{
           tableView.scrollEnabled = NO;
}


- (void)textViewDidEndEditing:(UITextView *)textView{
 tableView.scrollEnabled = YES;
}

2) Keyboard Notifications

- (void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated {
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}

- (void)keyboardWillShow:(NSNotification *)notif {
    tableView.scrollEnabled = NO;
}

- (void)keyboardWillHide:(NSNotification *)notif {
    tableView.scrollEnabled = YES;
}


来源:https://stackoverflow.com/questions/22447614/how-do-i-stop-a-tableview-from-scrolling-when-the-keyboard-appears

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!