我正在开展一个项目,其中有许多UIViewController
和所有都处于纵向模式。但是,想要激活特定UIViewController的旋转。
我之前搜索过这个并找到了很多答案,但所有这些都不适合我。我的应用程序也支持iOS6和iOS7。
此外,我已尝试使用此链接上提到的自定义NavigationController
How to allow only single UIViewController to rotate in both Landscape and Portrait direction?
请帮助我。谢谢。
答案 0 :(得分:0)
如果你的app只允许项目plist中的Device Orientation Portrait,这可能适合你。
表示您要旋转的特定viewcontroller.m
添加此方法:
- (BOOL)canAutoRotate
{
return YES;
}
然后在AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UIViewController *currentViewController = [self topViewController];
if ([currentViewController respondsToSelector:@selector(canAutorotate)]) {
NSMethodSignature *signature = [currentViewController methodSignatureForSelector:@selector(canAutorotate)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(canAutorotate)];
[invocation setTarget:currentViewController];
[invocation invoke];
BOOL canAutorotate = NO;
[invocation getReturnValue:&canAutorotate];
if (canAutorotate) {
return UIInterfaceOrientationMaskAll;
}
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController *)topViewController
{
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController
{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
答案 1 :(得分:0)
首先确保您已启用项目部署信息中的所有方向。
将以下代码添加到AppDelegate,
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
// Get topmost/visible view controller
UIViewController *currentViewController = [self topViewController];
if([currentViewController isKindOfClass:[ViewControllerThatWantsOrientation class]])
return UIInterfaceOrientationMaskAllButUpsideDown;
// Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController*)topViewController
{
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController* nav = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:nav.visibleViewController];
}
else if (rootViewController.presentedViewController)
{
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
}
else
{
return rootViewController;
}
}
在AppDelegate中添加上述代码后,从NavigationController类别和viewcontrollers中删除诸如shouldAutorotate,supportedInterfaceOrientations之类的方法(如果有的话)。
我希望这可以帮助你。
答案 2 :(得分:0)
在您的应用中delegate .h
添加以下代码行:
@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
BOOL flagOrientationAll;
}
@property (assign) BOOL flagOrientationAll;
在您的app delegate .m文件中添加以下方法
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
if([UICommonUtils isiPad]){
return UIInterfaceOrientationMaskAll;
}else if(flagOrientationAll == YES){
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
在视图中实现以下方式,您希望在iPhone设备中以纵向和横向旋转
-(void)viewWillAppear:(BOOL)animated
{
self.tabBarController.delegate = self;
PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}
}
-(void)viewWillDisappear:(BOOL)animated
{
//NSLog(@"viewWillDisappear -- Start");
PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}
检查我的帖子:iOS 7 Interface Orientation