I have a UIViewController with a custom UITextView in it (CodeTextView) and left of this text view I have a custom UITableView (LinesTableView). LinesTableView contains the line numbers aligned to the CodeTextView.
When I start editing the CodeTextView and when the keyboard appears, I edit the size of both views:
- (void)keyboardAppears:(NSNotification *)notification
{
[_codeTextView setFrame:CGRectMake(30, 0, 290, self.view.frame.size.height - 216.0f)];
[_linesTableView setFrame:CGRectMake(0, 0, 30, self.view.frame.size.height - 216.0f)];
}
When editing the CodeTextView and when I press a button, I update the LinesTableView:
- (void)updateLines
{
NSString *plainText = self.text;
NSArray *components = [plainText componentsSeparatedByString:@"\n"];
int lines = components.count;
if([[components lastObject] isEqualToString:@""]) lines--;
_linesTableView.lines = lines;
[_linesTableView reloadData];
}
However, when the keyboard is shown and when I press enter, the CodeTextView gets resized to it’s original height! When I comment out the [_linesTableView reloadData], everything is working fine. So somehow LinesTableView and CodeTextView gets their original heights back when I reload the LinesTableView.
I don’t know how to fix this. I think it’s not hard but I can’t come up with a way to do this. Could anyone please help me with this?
Thanks in advance! 🙂
Quick and dirty way would be to put both things in a UIView that wraps them, so you can resize that UIView and make it clip it’s contents, intead of resizing the actual table. That way, when the data is reloaded, even if the view scales up again it’ll remain clipped. In my head, it would even look better if you use a UIScrollView and put the 2 non-scrolling components in it. That way the place occupied by the keyboard doesn’t ruin the experience that much.