2013-09-30 1 views
10

Я попытался следующие методы, чтобы заставить пейзаж на одного моего просмотров:сила пейзаж Ios 7

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    return UIInterfaceOrientationLandscapeLeft; 
} 

- (BOOL)shouldAutorotate{ 
    return YES; 
} 

Ни сделал работу. Обратите внимание, что я тестирую симулятор и iPad.

Благодаря

ответ

22

Вот как я заставил одного из моих взглядов быть Пейзаж с помощью NavigationViewController:

  1. Реализован этот ответ: https://stackoverflow.com/a/12662433/2394787

  2. Импортного сообщения Вида контроллера: ObjC/message.h

  3. Эта строка кода в методе viewDidLoad добавлена:

objc_msgSend ([UIDevice CurrentDevice], @selector (setOrientation :), UIInterfaceOrientationLandscapeLeft);

Надеюсь, что это поможет кому-то.

+0

Спасибо, работал отлично! – iCode

+0

Будет ли эта логика одобрена для Apple? –

+0

Я использовал его в одном своем приложении, и это было принято. –

7

Принятый ответ, похоже, не работает на iOS 7.1.2. (Он работает на тренажере, но не работает во время работы на устройстве.)

Это, кажется, работает, хотя:

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; 

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"]; 
+0

мое решение работает в iPhone 4 с iOS 7.1.2. Каково ваше устройство? –

+0

iPhone 5s, iOS 7.1.2 –

1
int shouldShowInLandscape = 0; 

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 


    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(forceToLandscape:) 
               name:@"yourNameNotification" 
               object:nil]; 

} 

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

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
       return UIInterfaceOrientationMaskLandscapeLeft; 
    } else { 
      if(shouldShowInLandscape) 
      { 
       return UIInterfaceOrientationMaskLandscape; 
      } 
      return UIInterfaceOrientationMaskPortrait; 
    } 
} 

-(void) forceToLandscape:(NSNotification*) theNot 
{ 
    shouldShowInLandscape = [[theNot object] intValue]; 

} 

// от UIViewController

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"yourNameNotification" 
    object:[NSString stringWithFormat:@"1"]]; 
} 

-(void) viewDidDisappear:(BOOL)animated 
{ 

     [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"yourNameNotification" 
    object:[NSString stringWithFormat:@"0"]]; 

} 

// удаляем вас уведомлениеЦентр