2

내 응용 프로그램은 모든 4 방향을 지원합니다. UIViewControllerLandscapeRight입니다. UIViewController을 밀어 넣으려면 UINavigationController을 사용하고 있는데, UIViewControllerUIInterfaceOrientationLandscapeRight에만 있어야하지만 전화를 돌리면 다른 방향으로 다시 바뀝니다.iOS - 특정 UIViewController를 특정 방향으로 잠급니다.

-(BOOL)shouldAutorotate{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

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

이 당신의 마음을 확인, 당신은 현재 또는 푸시 사용하고 있습니까? – matt

답변

7

shouldAutorotate, supportedInterfaceOrientations 및 preferredInterfaceOrientationForPresentation을 제거하십시오.

가로보기 전용으로 표시하려는 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