我在UITabBarController中有5个导航控制器。UITabBarController 中的一个导航控制器有一个视图控制器始终横向其他所有纵向。
-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
上面的代码我放在导航控制器子类中。
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
上面的代码我放在景观控制器中。
我有 Base 作为 UIViewController 子类,因为我把这段代码放了
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if ([self isKindOfClass:[LanscapeViewController class]])
return UIInterfaceOrientationLandscapeLeft;
else
return UIInterfaceOrientationPortrait;
}
如果我呈现该视图控制器,它可以很好地显示该屏幕仅横向。但是如果我将视图控制器推入仍然在纵向中。
您是否在 ViewController 中为纵向模式编写以下代码。
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortraitUpsideDown | UIInterfaceOrientationPortrait;
}
看看这篇文章。http://www.klecker.de/photo/index.php?option=com_content&task=view&id=148&Itemid=213或在横向模式下显示导航控制器不起作用 ios 6.0分别。
我确实在标签栏应用程序中这样做了,但我没有强制旋转其中一个选项卡的直接根视图,而是强制旋转其中一个子视图。我不确定这是否直接适用于选项卡栏的视图控制器之一。
创建一个UIViewController
类别来检查当前的 ViewController,
#import <UIKit/UIKit.h>
@interface UIViewController (Utils)
+(UIViewController*) currentViewController;
@end
#import "UIViewController+Utils.h"
@implementation UIViewController (Utils)
+(UIViewController*) findBestViewController:(UIViewController*)vc {
if (vc.presentedViewController) {
// Return presented view controller
return [UIViewController findBestViewController:vc.presentedViewController];
} else if ([vc isKindOfClass:[UISplitViewController class]]) {
// Return right hand side
UISplitViewController* svc = (UISplitViewController*) vc;
if (svc.viewControllers.count > 0)
return [UIViewController findBestViewController:svc.viewControllers.lastObject];
else
return vc;
} else if ([vc isKindOfClass:[UINavigationController class]]) {
// Return top view
UINavigationController* svc = (UINavigationController*) vc;
if (svc.viewControllers.count > 0)
return [UIViewController findBestViewController:svc.topViewController];
else
return vc;
} else if ([vc isKindOfClass:[UITabBarController class]]) {
// Return visible view
UITabBarController* svc = (UITabBarController*) vc;
if (svc.viewControllers.count > 0)
return [UIViewController findBestViewController:svc.selectedViewController];
else
return vc;
} else {
// Unknown view controller type, return last child view controller
return vc;
}
}
+(UIViewController*) currentViewController {
// Find best view controller
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
return [UIViewController findBestViewController:viewController];
}
@end
在Appdelegate.m
导入UIView控制器类别上方,导入要在横向中显示的视图控制器,并将方向方法实现为
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[UIViewController currentViewController] isKindOfClass:[YourLandScapeViewController class]]) {
return UIInterfaceOrientationMaskLandscape; //Orientation for LandScapeViewcontroller
}
return UIInterfaceOrientationMaskPortrait; // rest All ViewController
}