MdaG
MdaG

Reputation: 2730

How do I lock my background image in place when rotating device?

I'd like to "lock" my background image (a UIImageView) such that it doesn't look like it's getting cropped when I rotate my device. It looks correct in landscape view, but doesn't follow along with the rotation.

I've tried stretching it using setAutoresizingMask, but the results look horrible. I've fiddled around with the transform property of the UIImageView, but fail to make it look correct.

Any pointers would be appreciated. I'm not even sure if I'm on the right track.

Upvotes: 0

Views: 302

Answers (2)

MdaG
MdaG

Reputation: 2730

OK, I managed to make it work using the following code:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    ...
        UIColor *color = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]];
        self.background.backgroundColor = color;
        [color release];
    ...
    }

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                duration:(NSTimeInterval)duration {
    float angle = (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) ? M_PI_2 : 0.0;       
    self.background.transform = CGAffineTransformMakeRotation(angle);
}

Bear in mind that background~ipad.png is 1024x768, that is landscape as default. If your image is portrait you need to check for landscape instead of portrait in the conditional cases.

Upvotes: 0

jbat100
jbat100

Reputation: 16827

You can have different images for portrait/landscape orientations and switch between the two with UIView animations on willRotate callbacks. I tried it and the result is quite smooth.

Upvotes: 2

Related Questions