I have a UINavigationController
where all UIViewController
in the stack are portrait, except the last one, which is landscape. My current implementation shows the last view controller in portrait on push, but I can rotate to landscape, and the, cannot rotate back to portrait. How can I force the rotation to landscape when the view is pushed ?
My UINavigationController
subclass :
@interface RotationViewController : UINavigationController
@end
@implementation RotationViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.topViewController.class == [LoadingViewController class])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if (self.topViewController.class == [LoadingViewController class])
{
return UIInterfaceOrientationLandscapeLeft;
}
return UIInterfaceOrientationPortrait;
}
@end
The view controller that I want landscape-only :
@implementation LoadingViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
@end
Other view controllers don't implement any of these methods.
I have the same condition. So You can implement the below code in your application
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
[[self navigationController].view setTransform:landscapeTransform];
self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 480, 44.0);
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
[[self navigationController].view setTransform:landscapeTransform];
self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320, 480);
self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 320.0, 44.0);
}
#define degreesToRadian(X) (M_PI * (X)/180.0)
Note:-
The above code is implement for the iphone. So Please change the bounds if you use this for ipad.
You need not to implement the
- (BOOL)shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
functions in your view controller.... So remove those methods....
Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need.
Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). Add the following method in this custom navigation controller
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
and add to your view controller that should rotate:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. Then, if you want to block some orientations for a particular view:
– application:supportedInterfaceOrientationsForWindow:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.