Question

My iPhone application is navigation-based and contains a lot of portrait-only views and one landscape-only view for viewing images. So I'd like to force this landscape-only view to automatically rotate to lanscape even if device is positioned in portrait mode.

So here is what I'w done in that view's controller:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
}

- (void)viewWillAppear:(BOOL)animated
{
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
 [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

 _originalTransform = [[appDelegate navigationController].view transform];
 _originalBounds = [[appDelegate navigationController].view bounds];
 _originalCenter = [[appDelegate navigationController].view center];

 CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
 landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);

 [[appDelegate navigationController].view setTransform:landscapeTransform];

 [appDelegate navigationController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
 [appDelegate navigationController].view.bounds  = CGRectMake(0.0, 0.0, 480.0, 320.0);
 [appDelegate navigationController].view.center  = CGPointMake (240.0, 160.0);

 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
 }
}

- (void)viewWillDisappear:(BOOL)animated
{ 
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
 [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 [[appDelegate navigationController].view setTransform:_originalTransform];
 [[appDelegate navigationController].view setBounds:_originalBounds];
 [[appDelegate navigationController].view setCenter:_originalCenter];

 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; 
}

Now I've got that view always shown in lanscape mode. But there is one problem. This view is positioned incorrectly as shown in this screenshot.

I tried to manually set the frame after rotation but it didn't help. Also I didn't find a complete guide how to set one view to be landscape-only. Please help me with this problem.

Was it helpful?

Solution

Check out this guy's post. Basically you need to transform around the center of the window. Looks like you may also need to rotate the status bar.

A different method would be to use UIDevice setDeviceOrientation, but that's a private API and thus not a great solution.

OTHER TIPS

Try using this method after you have applied the transformation:

[self.view sizeToFit];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top