问题
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