2013-08-28 2 views
0

Моего требования это мой первый ViewController открыто в портретном режиме only.and, когда пользователь переходит на 2-й ViewController я хочу, чтобы контроллер в ландшафтном режиме, как могу я сделать этопервого ViewController в портретном и SecondViewController в ландшафтном режиме

я пытался этот код

первый 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); 
} 

это не будет работать для меня.

+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 объявлен в App делегатом проверить погоды Первый контроллер представления или secondView контроллер isLandscape является 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; 
} 
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

этот код для категории, так как я могу это яблоко это на моем первом взгляде и втором взгляде ... – Jitendra

+0

вы можете использовать его, используя экземпляр класса UINavigation для вызова метода выше. –

0

паста следующего кода в ViewController .m файл второго контроллера представления (по @implementation секции)

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

Теперь выберите второй контроллер представления в раскадровке (Выбор обозначенной синей каймой вокруг зрения контроллера), перейдите к Атрибут инспектор (правая сторона «щит», как изображение) меняет ориентацию на пейзаж .. Вот и все .. Скажите мне, если это не работает для u. .. :)

Смежные вопросы