Fire Fist
Fire Fist

Reputation: 7060

Resize UITextView when rotation in iOS

Hello i am facing a problem with UITextView.

In my app , i need to support both Portrait and Landscape.

So i added UITextView into my app and resize the frame of UITextView when keyboard appearing with following code because when keyboard appear , the UITextView is behind the keyboard and i can't see what i write in UITextView.

- (void)keyboardWillShow:(NSNotification*)notification
{
    [self moveTextViewForKeyboard:notification up:YES];
}

- (void)keyboardWillHide:(NSNotification*)notification
{
    [self moveTextViewForKeyboard:notification up:NO];
}

- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardRect;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];

    if (up == YES)
    {
        CGFloat keyboardTop = keyboardRect.origin.y;
        CGRect newTextViewFrame = self.txtView.frame;
        newTextViewFrame.size.height = keyboardTop - self.txtView.frame.origin.y - 10;
        self.txtView.frame = newTextViewFrame;
    }

    else
    {
        self.txtView.frame = originalCGRect;
    }

    [UIView commitAnimations];
}

That is fine when keyboard appear and not covering my UITextView anymore. However when i rotate into Landscape , the UITextView frame is not correct anymore and also when i hide keyboard , it still remain like small size and not the fit with View.

enter image description here

enter image description here

How can i solve that problem?

Please help me.

Upvotes: 1

Views: 942

Answers (1)

Jitendra
Jitendra

Reputation: 5081

can you trying to see Autoresizing mask try this one

Try this i hope helpful to you....

Upvotes: 1

Related Questions