为什么我不能在使用UINavigationController时强制横向方向?

时间:2013-02-02 03:26:26

标签: ios objective-c uinavigationcontroller uiinterfaceorientation

我发现很多问题迫使UIViewController默认为横向: How to force a UIViewController to Portrait orientation in iOS 6 试试这个:

- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

但是当我在UINavigationController中使用UIViewController时,我无法强迫横向。请帮忙!

*工作不正常

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    [navControl setNavigationBarHidden:YES];
    self.window.rootViewController = navControl;

    [self.window makeKeyAndVisible];
    return YES;
}

*工作正常:

self.window.rootViewController = self.viewController;

2 个答案:

答案 0 :(得分:4)

最好的方法是创建扩展UINavigationController并在扩展类中编写方向函数。

班级宣言:

@interface MyNavigationControllerViewController : UINavigationController

@property(nonatomic, assign) UIInterfaceOrientation orientation;
@property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
@end

MyNavigationController的实现

@implementation MyNavigationController

@synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin;

@synthesize orientation = _orientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
        _orientation = UIInterfaceOrientationLandscapeLeft;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return _supportedInterfaceOrientatoin;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return self.orientation;
}

@end

并使用扩展名MyNavigationController作为rootviewcontroller的导航控制器。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) MyNavigationControllerViewController *myNavController;

- (void) reloadAppDelegateRootViewControllerLandscape;
- (void) reloadAppDelegateRootViewController;
@end

因此,您的应用程序代表将完成配置代码,如下所示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
    [self.myNavController setNavigationBarHidden:YES];
    self.window.rootViewController = self.myNavController;

    [self.window makeKeyAndVisible];
    return YES;
}

只有这样,您可以使用相同的导航控制器为两个不同的视图提供不同的方向,即重新加载导航控制器本身。所以如果你添加两个方法

- (void) reloadAppDelegateRootViewController{
    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait];
    [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

- (void) reloadAppDelegateRootViewControllerLandscape{
    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft];
    [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

并在推送和弹出视图后调用这些函数。

注意: - 我不知道这是好方法还是坏方法。

答案 1 :(得分:-3)

您需要转到plist文件并设置方向。您也可以在项目文件中执行此操作。 plist方向设置将覆盖所有。

在您的plist中,您可以将两个方向选项设置为左侧横向按钮和右侧横向按钮。