質問

I have a tableview that requires a subview that's used to add content. The subview contains a UITextView that's used to enter text. Prior to 7.1 The following would happen:

  • The subview is glued to the bottom of the tableview via scrollViewDidScroll
  • When activated, the keyboard would show, the view animates up with the keyboard
  • When text is typed in, the textView would expand upwards if required, with the bottom of the textView remaining in place.

NOW after 7.1, the following behavior takes place:

  • The subview animates upwards with the keyboard fine
  • If the text forces a new line, instead of expanding, textViewDidEndEditing get's triggered and the keyboard dismisses.
  • No further editing is allowed in the textview, any edits makes the keyboard immediately dismiss.

Any suggestions on what changed with 7.1 that would make existing, working code break? Here is all the relevant code. I'll be happy to provide any additional details. I've been at this a while now and it's driving me mental.

tableViewController.m - The code that adds in the subview. The subview has it's own xib. The xib was created in IB. I stripped out the parts that pertain to other components.

- (void)viewDidLoad {
    //Setup contentBar
    if (self.contentBar == nil) {
        //Add subView to View
        NSArray *subviewArray = [[NSArray alloc] init];
        subviewArray = [[NSBundle mainBundle] loadNibNamed:contentIdentifier owner:self options:nil];
        self.contentBar = [subviewArray objectAtIndex:0];
        [self.view addSubview:self.contentBar];

        //Setup textView
        UITextView *addContentTextView = (UITextView *)[self.contentBar viewWithTag:2];
        addContentTextView.text = [NSString stringWithFormat:@"text (optional)"];
        addContentTextView.delegate = self;
    }
}

My textView delegates

#pragma mark - TextView Delegate

- (void)textViewDidEndEditing:(UITextView *)textView{
    NSLog(@"textViewDidEndEditing:");

    if ([textView.text isEqualToString:@""]) {
        textView.text = @"text (optional)";
        textView.textColor = [UIColor lightGrayColor]; //optional
    }
    [textView resignFirstResponder];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    NSLog(@"textViewDidBeginEditing:");

    if ([textView.text isEqualToString:@"text (optional)"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor]; //optional
    }

    [textView becomeFirstResponder];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if ([newText length] > 0 || _image) {
        _addContentButton.enabled = YES;
    } else {
        _addContentButton.enabled = NO;
    }

    if([text isEqualToString:[text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]]) {
        return YES;
    } else {
        NSLog(@"return pressed");
        [textView resignFirstResponder];
        [self addedContent:nil];
    }

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"textViewDidChange");
    [self sizeTextView];
}

- (void)sizeTextView
{
    NSLog(@"sizeTextView");
    UIImageView *barImage = (UIImageView *)[self.contentBar viewWithTag:87];
    UITextView *textView = (UITextView *)[self.contentBar viewWithTag:2];

    CGSize sizeThatShouldFitTheContent = [textView sizeThatFits:textView.frame.size];
    CGRect newTextViewFrame = textView.frame;
    newTextViewFrame.size.height = sizeThatShouldFitTheContent.height;
    CGFloat adjustment = sizeThatShouldFitTheContent.height - textView.frame.size.height;
    newTextViewFrame.origin.y = textView.frame.origin.y - adjustment;
    textView.frame = newTextViewFrame;

    CGRect newImageViewFrame = barImage.frame;
    newImageViewFrame.size.height = barImage.frame.size.height + adjustment;
    newImageViewFrame.origin.y = barImage.frame.origin.y - adjustment;
    barImage.frame = newImageViewFrame;
}

If you want to see anything else, I'll be glad to post it.

役に立ちましたか?

解決

I have solved this issue by implementing the following:

  • Rewrote my tableview controller as a standard ViewController, not a TableViewController
  • Added a tableview to the ViewController and a separate "contentWrapper" view.
  • The content wrapper consists of the example shown here: https://github.com/datwelk/RDRStickyKeyboardView

I'm still not sure what in iOS 7.1 caused this issue, but using the RDRStickyKeyboardView works great and solved all of my issues.

I've read that tableviews shouldn't be parents of other views, so this solution also works well for good coding practice.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top