2013-08-28 80 views
0

我的要求是在人像模式这是我的第一个视图 - 控制开only.and当用户进入第二视图 - 控制我想在风景模式控制器如何可以我这样做1日的ViewController在纵向和SecondViewController在横向模式

我试着这个代码

1 ViewController.m

- (BOOL)shouldAutorotate 
{ 
    returnc YES; 
} 

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

    return UIInterfaceOrientationMaskPortrait; 

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

    return (interface==UIInterfaceOrientationMaskPortrait); 
} 

守则第2 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); 
} 

这对我来说工作不正常。

+0

有什么不合适呢?描述你遇到的问题。 –

+0

当用户去第二个控制器显示protrait模式我希望它在横向模式 – Jitendra

+0

我回答了类似的问题,它可以帮助。 http://stackoverflow.com/questions/18185260/view-controllers-in-view-controller-and-orientation-ios/18185775#18185775 – BoranA

回答

0

我正在解决我的问题,使用类别.... 添加新文件,并选择类别,并使子类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在应用程序委托声明来检查天气首先视图控制器或secondView控制器isLandscape是布尔为类别的代码。

现在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; 
} 
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 
+0

此代码是为类别,所以我怎么appley这个在我的firstview和secondview ... – Jitendra

+0

你可以通过使用你的UINavigation类实例来调用上面的方法来使用它。 –

0

膏继第二视图控制器的视图控制器.m文件代码(在@implementation部分)

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

现在选择故事板(由围绕视图控制器蓝色边框表示选择)第二视图控制器,转到属性检查器(右侧'盾'像图像)改变方向为风景..就是这样..告诉我,如果它不适合你。 .. :)

相关问题