MikeS
MikeS

Reputation: 3921

UIToolbar Rotation/Animation

I have a UIToolbar for an iPad app that is meant to stay at the top of the screen for all orientations. The UIView that my UIToolbar is part of does not rotate, however. The UIToolbar DOES rotate, but I'm having trouble getting the rotations to work the way I want...

I can't figure out how to change the size of the toolbar without stretching the contents of the toolbar (setting a scale transformation) or without messing up the transformations (setting the frame or the bounds).

- (void)updateOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated
{   
    CGAffineTransform toolbarTransform;
    switch(orientation) {
        case UIDeviceOrientationPortrait:
            toolbarTransform = CGAffineTransformIdentity;
            break;

        case UIDeviceOrientationLandscapeRight:
        toolbarTransform = CGAffineTransformMakeTranslation(-768 / 2 + 44 / 2, 768 / 2 - 44 / 2 + 1024 - 768);
        toolbarTransform = CGAffineTransformRotate(toolbarTransform, degreesToRadian(-90));

        break;

        case UIDeviceOrientationLandscapeLeft:
        toolbarTransform = CGAffineTransformMakeTranslation(768 / 2 - 44 / 2, 768 / 2 - 44 / 2);
        toolbarTransform = CGAffineTransformRotate(toolbarTransform, degreesToRadian(90));

        break;

        case UIDeviceOrientationPortraitUpsideDown:
            toolbarTransform = CGAffineTransformMakeTranslation(0, 1024 - 44);
            toolbarTransform = CGAffineTransformRotate(toolbarTransform, degreesToRadian(180));
            break;


        default:
            toolbarTransform = CGAffineTransformIdentity;
            break;
    }

    if(animated)
    {
        [UIView animateWithDuration:.5 animations:^
        {
            self.toolbar.transform = toolbarTransform;
        }];
    }
    else
    {
        self.toolbar.transform = toolbarTransform;
    }
}

This code mostly positions the toolbar how I want, but what should I do to resize the toolbar without messing up the transformations or stretching the toolbar's contents? Or maybe I'm approaching this wrong altogether?

edit: Slightly updated my code. Positioning is correct, just need to resize them somehow...

Upvotes: 0

Views: 763

Answers (1)

MikeS
MikeS

Reputation: 3921

The solution to this issue was to create a special UIView (called rotationView) and performing all rotation/translations to that UIView instead of directly to the UIToolbar. Then, I can change the toolbar.frame safely without "messing up" the transformations.

Upvotes: 1

Related Questions