Search code examples
iosuitabbarcontrolleruitextviewautoresizingmask

UITextview frame resized to incorrect height


I have a tabbar at the bottom of my view which I would like to hide. I am hiding it with the code provided by Saurabh, here.

The code works great, however I've added a line to change the location of my text view. But the text view gets resized! Here is the code:

- (void) hideTabBar:(UITabBarController *) tabbarcontroller {


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, 480.0f, view.frame.size.width, view.frame.size.height)];
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480.0f)];
        }

    }
    [buttonView setFrame:CGRectMake(0.0, 154.0, 320.0, 306.0)];
    [screenImageView setFrame:CGRectMake(0.0, 16.0, 320.0, 139.0 )];
    [inputView setFrame:CGRectMake(20.0f, 105.0f, 280.0f, 50.0f)];

    [UIView commitAnimations];

}

- (void) showTabBar:(UITabBarController *) tabbarcontroller {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {        
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, 431.0f, view.frame.size.width, view.frame.size.height)];

        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431.0f)];
        }


    }
    [buttonView setFrame:CGRectMake(0.0, 105.0, 320.0, 306.0)];
    [screenImageView setFrame:CGRectMake(0.0, 16.0, 320.0, 90.0 )];
    [inputView setFrame:CGRectMake(20.0f, 56.0f, 280.0f, 50.0f)];

    [UIView commitAnimations]; 

}

Every thing works great except the inputView (a UITextview) gets resized. If the tabbar is hidden, the height of the textview (inputView) becomes 58, and when I show the tabbar again, the height of the textview becomes 43.

I could always just add or subtract 7, or 8 as needed. But I thought it might be good to know what is causing this.

Thanks!


Solution

  • Most likely it is because the autoresizingMask property of your UITextView is set to allow for a flexible height. Something like the code below should help:

    inputView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin; // Pins inputView to the top