Wie man die Höhe von UITextview entsprechend der Textlänge erhöht, wie bei whatsapp

Ich habe die Chat-Funktion in meiner iOS-App mithilfe von Websocket implementiert und es funktioniert einwandfrei für mich.

Das Problem ist, ich möchte die Höhe des UITextView erhöhen, und das UITableView sollte sich entsprechend der Höhe des UITextView erhöhen. Kann mir jemand helfen

Below ist mein Code, den ich in meiner Anwendung verwende:

- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];

    [super viewDidAppear:animated];


    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.messagesTextView addGestureRecognizer:tgr];

    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillChangeFrameNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
        CGRect endFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        UIViewAnimationCurve curve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
        CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

        UIViewAnimationOptions options = curve << 16;

        [UIView animateWithDuration:duration delay:0.0 options:options animations:^{

            NSLog(@"frame: %@", NSStringFromCGRect(endFrame));


            CGRect frame = self.txtView.frame;
            if (isMsgLong==TRUE) {
                frame.origin.y = self.view.frame.size.height- CGRectGetHeight(self.txtView.frame)-216;

            }
            else {

                frame.origin.y = CGRectGetMinY(endFrame) - CGRectGetHeight(self.txtView.frame);
            }
            self.txtView.frame = frame;


            CGRect frame1 = self.sendButton.frame;
            frame1.origin.y = CGRectGetMinY(endFrame) - CGRectGetHeight(self.sendButton.frame);
            self.sendButton.frame = frame1;


            frame = self.messagesTextView.frame;
            frame.size.height = CGRectGetMinY(self.txtView.frame) - CGRectGetMinY(frame);
            self.messagesTextView.frame = frame;

            NSLog(@"did appear: %@", NSStringFromCGRect(self.txtView.frame));

        } completion:nil];
    }];

}

- (void)keyboardWillShow:(NSNotification *)note
{

    [UIView animateWithDuration:0.3 animations:^{
        NSDictionary *info = note.userInfo;
        NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
        CGRect rawFrame = [value CGRectValue];
        CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];

        NSLog(@"frame: %@", NSStringFromCGRect(self.txtView.frame));

        self.txtView.transform = CGAffineTransformMakeTranslation(0, -keyboardFrame.size.height);

        self.sendButton.transform = CGAffineTransformMakeTranslation(0, -keyboardFrame.size.height);
        self.messagesTableView.contentInset=UIEdgeInsetsMake(0, 0,keyboardFrame.size.height,0);
        //keyboardFrame.size.height

        NSLog(@"frame1: %@", NSStringFromCGRect(self.txtView.frame));

        isMsgLong = FALSE;

    }];
}

- (void)keyboardWillHide:(NSNotification *)note
{
    [UIView animateWithDuration:0.3 animations:^{

        NSLog(@"frame2: %@", NSStringFromCGRect(self.txtView.frame));
        self.txtView.transform = CGAffineTransformIdentity;


        NSLog(@"frame3: %@", NSStringFromCGRect(self.txtView.frame));

        self.sendButton.transform = CGAffineTransformIdentity;
        self.messagesTableView.transform = CGAffineTransformIdentity;

        isMsgLong = TRUE;
    }];
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    self.messagesTableView.contentInset=UIEdgeInsetsMake(0, 0, 0, 0);

    self.txtView.transform = CGAffineTransformMakeTranslation(0, -216);
    self.sendButton.transform = CGAffineTransformMakeTranslation(0, -216);


    NSLog(@"frame5: %@", NSStringFromCGRect(self.txtView.frame));


    NSInteger lastSectionIndex = [self.messagesTableView numberOfSections] - 1;
    NSInteger lastRowIndex = [self.messagesTableView numberOfRowsInSection:lastSectionIndex] - 1;
    if (lastRowIndex==-1) {
        lastRowIndex=0;
    }
    NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];

    if (lastRowIndex==0) {

    }
    else {
        [self.messagesTableView scrollToRowAtIndexPath:pathToLastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    //UITableViewScrollPositionMiddle
    }
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage