2013-08-28 178 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); 
} 

現在選擇故事板(由圍繞視圖控制器藍色邊框表示選擇)第二視圖控制器,轉到屬性檢查器(右側'盾'像圖像)改變方向爲風景..就是這樣..告訴我,如果它不適合你。 .. :)

相關問題