jadengeller
jadengeller

Reputation: 1327

Setting UIView Frame

I'm trying to resize my UITextView when the keyboard appears, but I don't know what I'm doing wrong.

I have these three lines of code called when the keyboard appears:

CGSize kbSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

textView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-kbSize.height);

NSLog(@"%f,%f,%f,%f", textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.frame.size.height);

For some really odd reason my keyboard doesn't appear and here is what is logged:

0.000000,-180.000000,480.000000,180.000000

Why is the CGRects y origin getting set to -180 when it is hardcoded in at 0?

EDIT: textView is a UITextView I created programatically. I didn't use Interface Builder.

EDIT2: It seems that the problem is that the view is autoresizing when rotated.

Upvotes: 0

Views: 129

Answers (1)

Alex Moskalev
Alex Moskalev

Reputation: 607

You can try this :

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

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

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

    CGRect newFrame = textView.frame;
    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
    keyboardFrame.size.height -= tabBarController.tabBar.frame.size.height;
    newFrame.size.height -= keyboardFrame.size.height * (up?1:-1);
    textView.frame = newFrame;

    [UIView commitAnimations];   
}

Hope it'll help

Upvotes: 1

Related Questions