4

I've implemented a ViewController that has a bottom UIView containing UITextView with scroll disabled that resizes as you type inside.

When the height of the text contained reaches 90 pixels, I enable scroll ->

scrollEnabled = YES;

What's supposed to happen: The UITextView and its superview should stay as the height that they were limited to (over the 90 pixel limit).

What happens: The UITextView resizes back to its default value.

More Info: I'm using the code of Multiline UITextField as my bottom view. I'm using iOS7.

Any help is appreciated, thank you.

EDIT: my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textBox.scrollEnabled = NO;
    self.textBox.font = [UIFont fontWithName:@"Helvetica" size:14];
    [self registerForKeyboardNotifications];
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWasShown:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification 
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector: @selector(keyPressed:)
                                                 name: UITextViewTextDidChangeNotification
                                               object: nil];
}

- (void)keyboardWasShown:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self setViewMovedUp:YES byHeight:kbSize.height];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self setViewMovedUp:NO byHeight:kbSize.height];
}

- (void)keyPressed:(id)sender
{
    CGRect textRect = [self.textBox.text boundingRectWithSize:CGSizeMake(255,MAXFLOAT) 
                                                      options:(NSStringDrawingUsesLineFragmentOrigin) 
                                                   attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:14]} 
                                                      context:nil];
    NSInteger newSizeH = textRect.size.height;
    if (self.textBox.hasText) {
        // if the height of our new chatbox is
        // below 90 we can set the height
        if (newSizeH <= 90) {
            self.textBox.scrollEnabled = NO;
            [self.textBox scrollRectToVisible:CGRectMake(0,0,1,1) 
                                     animated:NO];

            // chatbox
            CGRect chatBoxFrame = self.textBox.frame;
            chatBoxFrame.size.height = newSizeH + 12;
            self.textBox.frame = chatBoxFrame;

            // form view
            CGRect formFrame = self.commentBox.frame;
            formFrame.size.height = 30 + newSizeH;
            self.commentBox.frame = formFrame;
        }

        // if our new height is greater than 90
        // sets not set the height or move things
        // around and enable scrolling
        if (newSizeH > 90) {
            self.textBox.scrollEnabled = YES;
            CGRect frame = self.textBox.frame;
            frame.size.height = 102;
            self.textBox.frame = frame;
            CGRect formFrame = self.commentBox.frame;
            formFrame.size.height = 30 + 90;
            self.commentBox.frame = formFrame;
        }
    }
}

- (void)setViewMovedUp:(BOOL)movedUp byHeight:(CGFloat)height
{
    int movement = movedUp ? -height : height;
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.dataView.frame = CGRectOffset(self.dataView.frame, 0.0, movement);
     }];  
}
Community
  • 1
  • 1
user2558461
  • 281
  • 1
  • 6
  • 21
  • Scrolling doesn't resize UITextView and it's height. It can only move the text to another place. Can you explain more clearly how UITextView can resize, please? – Miroslav Dec 17 '13 at 11:43
  • The UITextView default height is 20. when I reach the height of 90 according to the text, all I do is set the scrolling on and it sets the height of the UITextView to 20 as well as its superview – user2558461 Dec 17 '13 at 11:57
  • So just set textView.frame.size.height = 90. Why doesn't it work? – Miroslav Dec 17 '13 at 18:46
  • I set it to 90, even higher, it doesn't respond – user2558461 Dec 17 '13 at 20:06
  • I reproduced all the steps you wrote - and everything is ok. Can you add to your question some code? I think the mistake is in it. – Miroslav Dec 18 '13 at 09:29
  • I added my keyPressed code, please take a look – user2558461 Dec 18 '13 at 12:48
  • I copied this code without changing anything. Everything still works for me. Dunno what can it be :( Try to operate with contentSize of your textView or just clean the project... – Miroslav Dec 18 '13 at 13:40
  • Well, I added the entire code related to the uitextview. I'm really out of luck... – user2558461 Dec 18 '13 at 14:06
  • 1
    @user2558461 Could you please upload a sample project with the problem? As a general rule, when something doesn't work, it is best to start a new, small project and try to reproduce there. Try that, and if it reproduces, please upload it so we can take a look. – Léo Natan Dec 20 '13 at 21:05
  • This is all my code regarding the textview. Nothing else in my code is related so it must be in the code above. No? – user2558461 Dec 21 '13 at 07:36

2 Answers2

1

Although I am a little late, but just incase. I faced the same problem myself. The text view came back to the original size on scroll. The way I resolved it was to update the height constraint on the UITextView. That is each time you update the size of the UITextView, you also need to update the corresponding constraints.

Sarfraz
  • 425
  • 2
  • 5
  • 19
0

Here is code that I use in iOS 6 and 7.

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {

    // I set _maxTextViewHeight based on device, but you can harcode it to 90
    CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, _maxTextViewHeight)];
    float desiredTextViewHeight = size.height - 7.5;
    float desiredInputViewHeight = MIN(desiredTextViewHeight + 16.0f, _maxInputViewHeight);

    textView.scrollEnabled = (desiredInputViewHeight == _maxInputViewHeight);

    NSRange bottom = NSMakeRange([textView.text length] - 1, 1);
    [textView scrollRangeToVisible:bottom];

    CGRect inputViewFrame = _inputView.frame;
    float heightDelta = desiredInputViewHeight - inputViewFrame.size.height;
    inputViewFrame.size.height = desiredInputViewHeight;
    inputViewFrame.origin.y -= heightDelta;

    if (heightDelta != 0) {
        [UIView animateWithDuration:0.1 animations:^{
            _inputView.frame = inputViewFrame;
        } completion:nil];
    }
}

Also you can take a look onto this project https://github.com/jessesquires/MessagesTableViewController

Sergey Kuryanov
  • 6,114
  • 30
  • 52
  • Thank you for your answer, I'll try it out. may I ask how did you set the _maxInputViewHeight based on the device? – user2558461 Dec 24 '13 at 12:34
  • I am write a chat. So in `keyboardWillAppear` I calculate max size of view: `_maxInputViewHeight = self.view.bounds.size.height - 44.0f - 20.0f - keyboardRect.size.height; if ([self.navigationItem.prompt length] > 0) _maxInputViewHeight -= 20.0f;` – Sergey Kuryanov Dec 24 '13 at 21:46
  • I'm trying your code but it still resizes. if I remove the line `textView.scrollEnabled = (desiredInputViewHeight == _maxInputViewHeight);` it doesn't resize, yet doesn't limit size. I tried creating a copy of the UIViewController to see if I made any error, but it doesn't seem like it. could you assist? – user2558461 Jan 08 '14 at 14:32