看看另一种方法:
http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/
您只需在 ViewController 中实现 canRotate 即可允许旋转。
在 iOS 7 上运行良好。
2015-01-30
因为塞巴斯蒂安的网站似乎无法正常工作(404 错误),这是我对其解决方案的解释:
与 sebastian 不同,我更喜欢使用协议(如 C# 中的接口)以避免在我的每个视图控制器中创建方法“-(void)canrotate:”。
IRotationCapabilities.h
-----------------------
#ifndef NICE_APPS_IRotationCapabilities_h
#define NICE_APPS_IRotationCapabilities_h
@protocol IRotationCapabilities < NSObject >
// Empty protocol
@end
#endif
FirstViewController.h
---------------------
- ( void )viewWillAppear:( BOOL )animated
{
[ super viewWillAppear:animated ];
// Forces the portrait orientation, if needed
if( ![ self conformsToProtocol:@protocol( IRotationCapabilities ) ] )
{
if( self.navigationController.interfaceOrientation != UIInterfaceOrientationPortrait )
{
[ [ UIDevice currentDevice ] setValue:@( 1 ) forKey:@"orientation" ];
}
}
}
SecondViewController.h
-----------------------
#import "IRotationCapabilities.h"
@interface SecondViewController : UIViewController < IRotationCapabilities >
AppDelegate.m
-------------
#pragma mark - Orientation management
- ( NSUInteger )application:( UIApplication * )application supportedInterfaceOrientationsForWindow:( UIWindow * )window
{
if( __iPhone )
{
// Gets topmost/visible view controller
UIViewController * currentViewController = [ self topViewController ];
// Checks whether it implements rotation
if( [ currentViewController conformsToProtocol:@protocol( IRotationCapabilities ) ] )
{
// Unlock landscape view orientations for this view controller
return ( UIInterfaceOrientationMaskAllButUpsideDown );
}
// Allows only portrait orientation (standard behavior)
return ( UIInterfaceOrientationMaskPortrait );
}
else
{
// Unlock landscape view orientations for iPad
return ( UIInterfaceOrientationMaskAll );
}
}