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?
You have to calculate the number of lines in your
textView
and trigger frameUpdate for each changes/line increment, which was discussed hereThen 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:
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 of44px
for your toolbar, you need to subtract the exceeds to the.origin.y
something like:Also if you are using
NSNotificationCenter
to update the frame of yourToolBar.. You need to store the original frame in a global variable for thekeyboardHideEvent
, so that you will be able to easily get the frame back to its original position ..Hope this helps you, Cheers! ;)