Make UIToolbar resize with resizing UITextView contained in toolbar

810 views Asked by At

I have a UIToolbar that lies at the bottom of my ViewController and moves to the top of the keyboard with animation as the keyboard slides up. In other words it moves with the keyboard. Now I am creating a way for the UITextView which is contained in the toolbar to resize it's height as the user types up newlines in the textView. So far I'm using this code in my class:

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGFloat fixedWidthToolbar = self.navigationController.toolbar.frame.size.width;

    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGSize newSizeToolbar = [self.navigationController.toolbar sizeThatFits:CGSizeMake(fixedWidthToolbar, MAXFLOAT)];

    CGRect newFrame = textView.frame;
    CGRect newToolbarFrame = self.navigationController.toolbar.frame;

    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    newToolbarFrame.size = CGSizeMake(fmaxf(newSizeToolbar.width, fixedWidthToolbar), newSizeToolbar.height);
    textView.frame = newFrame;
    self.navigationController.toolbar.frame = newToolbarFrame;
}

Everything works except the tool bar. By working, I mean that the UITextView grows whenever a newline is added to it(the user it typing lots of text). The problem here is that I'm not seeing the toolbar grow in size like the textView is. I believe my problem is that I used the same code for resizing the toolbar that I used for resizing the textview.

EDIT: The general idea here is to allow both the UITextView and the UIToolbar resize at the same time, allowing the user to see there text being written in the UITextView.

What can I do to achieve this goal? What am I doing wrong?

1

There are 1 answers

2
0yeoj On

You have to calculate the number of lines in your textView and trigger frameUpdate for each changes/line increment, which was discussed here

Then you need to update the frame simultaneously and again compute this based from the changes..

About the line update, your code could be looking like:

NSInteger linesPrevious = 0;//global variable to monitor changes

- (void)textViewDidChange:(UITextView *)textView
{
    [self checkNumberOfLines:YourTextView.text];
}

- (void)checkNumberOfLines:(UITextView *)textView
{
    //compute the number of lines
    NSInteger linesNew = textView.contentSize.height/textView.font.lineHeight;

    if (linesNew != linesPrevious)
    {
        // make updates here
    }
}

I suggest you calculate the textView.frame first, based from the current contentSize the followed by the UIToolbar.Frame

and dont forget to update the .origin.y or your Toolbar to keep it at the right position, assuming you have height of 44px for your toolbar, you need to subtract the exceeds to the .origin.y something like:

frame = YourToolBar.frame;
// example you updated the height with `64px`
frame.size.height = 64;

// you also need to update the `.origin.y`
frame.origin.y -= (frame.size.height/*new*/ - YourToolBar.frame.size.height/*current*/);

Also if you are using NSNotificationCenter to update the frame of yourToolBar.. You need to store the original frame in a global variable for the keyboardHideEvent, so that you will be able to easily get the frame back to its original position ..


Hope this helps you, Cheers! ;)