Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView scrolls too far when editing embedded UITextView
    primarykey
    data
    text
    <p>I've successfully embedded a UITextView inside a UITableViewCell, and most of the table is working fine. However, when I enable editing on the UITextView and start editing, the table shifts up too far, obscuring the cursor. Is there a way to control how far the tableview scrolls? Manually scrolling the view to the cell works, but the view still scrolls up before scrolling back down and into view.</p> <p>Here is an example of what I mean: editing a UITextField - the field is neatly placed directly above the keyboard. <a href="http://imageshack.us/photo/my-images/13/textfield.png/" rel="nofollow">http://imageshack.us/photo/my-images/13/textfield.png/</a></p> <p>editing a UITextView - the field is placed significantly above the keyboard removing the toolbar from the keyboard doesn't affect anything) <a href="http://imageshack.us/photo/my-images/809/textview.png/" rel="nofollow">http://imageshack.us/photo/my-images/809/textview.png/</a></p> <p>Here's all the code that is relevant to the UITextView in the UITableViewController object:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = self.editButtonItem; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil]; } - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { viewTargetedForEditing = (UIView *)textView; return YES; } - (void)textViewDidBeginEditing:(UITextView *)textView { [self.navigationController setNavigationBarHidden:YES animated:YES]; // shrink the textView to fit on-screen originalTextViewFrame = textView.frame; CGRect keyboardRect = [[keyboardUserinfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardHeight = keyboardRect.size.height; CGRect newViewFrame = textView.frame; CGFloat spaceAboveKeyboard = self.containerView.frame.size.height - keyboardHeight; newViewFrame.size.height = (spaceAboveKeyboard &lt; textView.frame.size.height) ? spaceAboveKeyboard : textView.frame.size.height; /* animate the calculations */ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:[[keyboardUserinfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; textView.frame = newViewFrame; [UIView commitAnimations]; // recalculate the keyboard height, in case we aren't in portrait mode CGFloat toolbarHeight; if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { toolbarHeight = 44.0; } else { toolbarHeight = 32.0; } CGRect frame = textView.inputAccessoryView.frame; frame.size.height = toolbarHeight; } - (void)endTextViewEditing { [viewTargetedForEditing resignFirstResponder]; } - (void)textViewDidEndEditing:(UITextView *)textView { [self.navigationController setNavigationBarHidden:NO animated:YES]; } #pragma mark - Keyboard Notifications - (void)keyboardWillShow:(NSNotification *)notification { [keyboardUserinfo release]; keyboardUserinfo = [[notification userInfo] retain]; [self viewDidBeginEditing:viewTargetedForEditing]; } - (void)keyboardWillHide:(NSNotification *)notification { [keyboardUserinfo release]; keyboardUserinfo = [[notification userInfo] retain]; [self viewDidEndEditing:viewTargetedForEditing]; } - (void)viewDidBeginEditing:(id)aView { // called 2nd UITableViewCell *cell = (UITableViewCell *)[aView superview]; [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } - (void)viewDidEndEditing:(id)aView { NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[viewTargetedForEditing superview]]; gallupAppDelegate *appDelegate = (gallupAppDelegate *)[[UIApplication sharedApplication] delegate]; switch (indexPath.section) { case SectionDescription: if (![[(UITextField *)aView text] isEqualToString:self.plan.Description]) { self.plan.Description = [(UITextField *)aView text]; [appDelegate saveManagedContext]; } break; case SectionNotes: if (![[(UITextView *)aView text] isEqualToString:self.plan.Notes]) { self.plan.Notes = [(UITextView *)aView text]; [appDelegate saveManagedContext]; } break; case SectionLocation: if (![[(UITextField *)aView text] isEqualToString:self.plan.Location]) { self.plan.Location = [(UITextField *)aView text]; [appDelegate saveManagedContext]; } break; } } </code></pre> <p>The TextViewCell is a subclass of UITableViewCell, and it is the cell that is giving me the problem with scrolling.Here's the implementation:</p> <pre><code>@implementation TextViewCell @synthesize contents=_contents; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { _contents = [[UITextView alloc] initWithFrame:CGRectZero]; [self addSubview:_contents]; self.contents.font = [UIFont systemFontOfSize:17]; self.contents.dataDetectorTypes = UIDataDetectorTypeAll; CGRect frame = CGRectMake(0, 0, self.window.frame.size.width, 44.0); UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(endTextViewEditing)]; UIToolbar *textViewToolbar = [[UIToolbar alloc] initWithFrame:frame]; textViewToolbar.barStyle = UIBarStyleBlackOpaque; [textViewToolbar setItems:[NSArray arrayWithObjects:spacer, done, nil]]; self.contents.inputAccessoryView = textViewToolbar; } return self; } - (void)endTextViewEditing { [self.contents resignFirstResponder]; } #define LEFT_MARGIN 15 #define TOP_MARGIN 5 - (void)layoutSubviews { [super layoutSubviews]; CGFloat width = self.frame.size.width - (LEFT_MARGIN *2); CGFloat height = self.frame.size.height - (TOP_MARGIN *2); self.contents.frame = CGRectMake(LEFT_MARGIN, TOP_MARGIN, width, height); } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { //[super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)dealloc { [super dealloc]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload