0

我的要求是这是我的第一个视图控制器仅在纵向模式下打开。当用户转到第二个视图控制器时,我希望该控制器在横向模式下我该怎么做

我试过这段代码

第一个 ViewController.m

- (BOOL)shouldAutorotate
{
    returnc YES;
}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    return UIInterfaceOrientationMaskPortrait;

}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
{

    return (interface==UIInterfaceOrientationMaskPortrait);
}

第二个 ViewController.m 的代码

- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;

    //return UIInterfaceOrientationMaskLandscape;
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

这对我来说不起作用。

4

3 回答 3

0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

更新:

您可以通过创建 UINaviagationController 类别来做到这一点

.h 文件的代码是

@interface UINavigationController (autorotation)

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;

.m 文件的代码是

 @implementation UINavigationController (autorotation)

    -(BOOL)shouldAutorotate
    {

        UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
        [self.topViewController shouldAutorotate];
        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAll;

    }
    @end
于 2013-08-28T10:44:33.930 回答
0

将以下代码粘贴到第二个视图控制器的 viewcontroller .m 文件中(在 @implementation 部分下)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

现在选择情节提要中的第二个视图控制器(视图控制器周围的蓝色边框表示选择),转到属性检查器(右侧“盾牌”像图像)将方向更改为横向..就是这样...如果没有,请告诉我不为你工作。..:)

于 2013-08-28T11:02:48.040 回答
0

我正在使用 Category.... 添加新文件并选择 Category 并创建子类 UINavigationController 类。

这是.h的类别代码

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"

    @interface UINavigationController (orientation)

    @end

code for .m file

#import "UINavigationController+orientation.h"

@implementation UINavigationController (orientation)

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{

    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    if (delegate.islandscape)
    {
        // for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
        return UIInterfaceOrientationMaskLandscape;

    }
    return UIInterfaceOrientationMaskPortrait;

}
@end

isLandscape 在 App 委托中声明以检查天气 First view controller 或 secondView Controller isLandscape is Bool。

现在 FirstViewController.m 文件我希望在 Portarit 模式下使用此代码

- (IBAction)PlayClicked:(id)sender
{
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];


    self.navigationController.navigationBarHidden=YES;
    delegate.islandscape=YES;

    ViewController * v=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

    [self presentViewController:v animated:NO completion:nil];


    //[self dismissViewControllerAnimated:YES completion:nil];
   [self.navigationController pushViewController:v animated:YES];

}


- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

和 SecondViewController 我希望在横向模式下使用这个。

delegate.islandscape=NO;   // called transfer to Category 

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
于 2013-08-28T12:17:27.440 回答