我在我的一个项目中旋转视图控制器时遇到了问题,我最终实现了这个解决方案,希望它会对你有所帮助。
- (void)viewDidLoad
{
[super viewDidLoad];
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
// Do any additional setup after loading the view.
}
-(BOOL) shouldAutorotate{
return NO;
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
CGRect rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);;
switch (deviceOrientation) {
case 1:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = [[UIScreen mainScreen] bounds];
[UIView commitAnimations];
break;
case 2:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(-M_PI);
self.view.bounds = [[UIScreen mainScreen] bounds];
[UIView commitAnimations];
break;
case 3:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
//rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = rect;
[UIView commitAnimations];
break;
case 4:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
//rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
self.view.bounds = rect;
[UIView commitAnimations];
break;
default:
break;
}
}
更新:
-(NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationPortrait;
}