在iOS中旋转时调整UITextView的大小

时间:2013-07-18 09:57:09

标签: ios cocoa-touch rotation uitextview cgrect

您好我遇到了UITextView的问题。

在我的应用程序中,我需要同时支持Portrait和Landscape。

所以我将UITextView添加到我的应用中,并在键盘出现时使用以下代码调整UITextView的框架大小,因为当键盘出现时,UITextView在键盘后面,我不能看看我在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];
}

当键盘出现并且不再覆盖我的UITextView时,这很好。 然而,当我旋转到横向时,UITextView框架不再正确,而且当我隐藏键盘时,它仍然保持像小尺寸而不适合View。

enter image description here

enter image description here

我该如何解决这个问题?

请帮帮我。

1 个答案:

答案 0 :(得分:1)

can you trying to see Autoresizing mask try this one

试试这个我希望对你有帮助....

相关问题