我有一个项目使用UINavigationController
和segues
工作得很好,所有这些项目都正确旋转,事情就是......我只是想在特定的{{}上禁用autorotation
1}}。
我试过这个:
UIViewController
但它不起作用,我的- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
// New Autorotation support for iOS 6.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0){
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
会自动轮换,欢迎任何帮助:)
答案 0 :(得分:35)
根据View Controller编程指南
如果要暂时禁用自动旋转,请避免操作方向遮罩来执行此操作。而是覆盖初始视图控制器上的shouldAutorotate方法。在执行任何自动旋转之前调用此方法。如果它返回NO,则旋转被抑制。
所以你需要继承'UINavigationController',实现shouldAutorotate并在故事板中使用你的导航控制器类。
- (BOOL)shouldAutorotate
{
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[DetailViewController class]])
return NO;
return YES;
}
答案 1 :(得分:15)
要完成GayleDDS对新手的回答 刚刚添加了一个UINavigationController的子类,如下所示:
#import "UINavigationController.h"
#import "MonthCalendarVC.h"
@implementation UINavigationController (overrides)
- (BOOL)shouldAutorotate
{
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[MonthCalendarVC class]])
return NO;
return YES;
}
@end
MonthCalendarVC是我想要处于纵向模式(已修复)的viewController,然后只是将导入添加到我的appdelegate.m
#import "UINavigationController.h"
就是这样
答案 2 :(得分:5)
看看这个其他方法:
http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/
您只需在ViewController中实现canRotate以允许旋转。
在iOS 7上正常运行。
<强>二○一五年一月三十〇日强> 因为塞巴斯蒂安的网站似乎不起作用(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 );
}
}
答案 3 :(得分:4)
尝试在你的UIViewController中实现它:
// implements the interface orientation (iOS 6.x)
@interface UINavigationController (RotationNone)
-(NSUInteger)supportedInterfaceOrientations;
@end
@implementation UINavigationController (RotationNone)
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
@end
答案 4 :(得分:0)
我在Swift中看到有人问这个问题。这不是立即显而易见的,因为Objective-C方法根本不是Swift中的方法,而是计算的vars:
override var shouldAutorotate: Bool { return false }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }