我的iPhone应用程序仅支持纵向方向。我想添加到仅支持横向方向的项目视图控制器?可能吗?如果是的话我怎么能实现呢?
我试图像这样包装类别文件:
@implementation UINavigationController (Rotation_IOS7)
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
如果我这样做,我会收到此错误:
由于未捕获的异常UIApplicationInvalidInterfaceOrientation
而终止应用,原因为:Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES
答案 0 :(得分:4)
我试过这个并且它有效:http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/
首先,将这些代码添加到AppDelegat类中。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// Get topmost/visible view controller
UIViewController *currentViewController = [self topViewController];
// Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
// Unlock landscape view orientations for this view controller
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:[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;
}
}
然后,在横向视图控制器中,添加此方法
- (void)canRotate { }
答案 1 :(得分:1)
我搜索了很多主题,最后找到了一个有效的解决方案。
在我的例子中,我有两个VC:
A - >嵌入在Nav中的VC。控制器,应该只支持纵向视图。
B - > VC未嵌入VC中且仅支持Landscape。
我想从视图A到视图B(通过按下按钮)然后返回查看A然后具有特定方向的A仍然正确。
予。为UINavigationController创建一个类别,并在其.m文件中写入以下内容:(代码将自动调用)
- (NSUInteger)supportedInterfaceOrientations
{
NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]);
return [self.topViewController supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
II。 在A和B之间创建一个模态segue,然后在B和A之间创建另一个模式segue。
III。在每个View Controllers .m文件中记下以下内容:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
OR
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
添加此代码后。您将能够更改单个视图B的方向。
答案 2 :(得分:0)
编辑:
在.h中创建一个类别,然后实现这些方法
在视图控制器中使用这些方法,以支持格局
@implementation UINavigationController (Rotation_IOS7)
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}