2

Мое приложение поддерживает каждую 4 ориентации, у меня есть UIViewController, который находится в LandscapeRight. Я использую UINavigationController, чтобы нажать UIViewController, я хочу, чтобы UIViewController был только в UIInterfaceOrientationLandscapeRight, но когда я поворачиваю телефон, он переключается на другую ориентацию.iOS - Заблокировать определенный UIViewController в определенной ориентации

-(BOOL)shouldAutorotate{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
    return UIInterfaceOrientationLandscapeRight; 
} 
+0

Решайтесь, вы используете присутствующая или толкать? – matt

ответ

7

просто удалите эти shouldAutorotate, поддерживаетсяInterfaceOrientations и preferInterfaceOrientationForPresentation.

и добавьте это в viewcontroller, который вы хотите отображать только ландшафт.

-(void)viewDidAppear:(BOOL)animated{ 

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

Фактически, это из-за аналогичного вопроса с решением здесь. How to force view controller orientation in iOS 8?

2

Вам необходимо создать подкласс класса UIViewController. И примените изменения ориентации интерфейса в этом подклассе. Расширьте свой контроллер представлений, в котором вы хотите заблокировать ориентацию с помощью подкласса. Я приведу пример этого.

У меня есть класс, который показывает только ориентацию ландшафта для контроллера вида.

LandscapeViewController является подклассом UIViewController, в котором вы должны иметь дело с ориентациями.

LandscapeViewController.h:

#import <UIKit/UIKit.h> 

@interface LandscapeViewController : UIViewController 

@end 

LandscapeViewController.m:

#import "LandscapeViewController.h" 

@interface LandscapeViewController() 

@end 

@implementation LandscapeViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(BOOL)shouldAutorotate { 
    return YES; 
} 

-(NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskLandscape; 
} 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     return YES; 
    } 
    else { 
     return NO; 
    } 
} 

@end 

Расширьте контроллер представления, используя выше подкласса.

Например:

#import "LandscapeViewController.h" 

@interface SampleViewController : LandscapeViewController 

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