Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 623
Kristoffer M Sanio Avatar asked Jan 12 '23 00:01

Kristoffer M Sanio


2 Answers

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

like image 73
Irfan Avatar answered Jan 20 '23 02:01

Irfan


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;
}
like image 35
Thukaram Avatar answered Jan 20 '23 00:01

Thukaram