본문 바로가기

iOS

[iOS] iOS 화면 자동 회전 제어(Autorotate) 하기

iOS 5.0 이전에서는 뷰 컨트롤러의 특정 방향만 회전하도록 제어하는 코드는 다음과 같았다.


#pragma mark iOS5 rotate

- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

    // 세로보기만 지원

    

    //return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

    //거꾸로 들기 빼고 허용

}


하지만 위의 shouldAutorotateToInterfaceOrientation: 메소드가 iOS 6.0 부터 Deprecated 되었다.


6.0에서는 화면의 자동회전 여부를 단순히 shouldAutorotate 란 메소드로 결정한다.

- (BOOL) shouldAutorotate

{

    return YES;//자동회전 허용

    //return NO; //자동회전 비허용

}


위 메소드가 YES를 리턴할 때, 즉 자동회전이 허용될때 회전히 허용되는 화면(세로, 가로 왼쪽, 가로 오른쪽, 거꾸로)의 설정은 xCode의 프로젝트 요약 화면(정확한 명칭을 모르겠음)에서 설정할 수 있다



Target 에서 Summary를 눌렀을 때 볼수 있는 화면이다.(보통 프로젝트를 생성하면 처음 볼수있는 화면)

기본적으로 위의 스샷에서처럼 세로, 가로 양옆이 눌려있으며 이것이 shouldAutorotate가 YES를 리턴 할 때 전환이 허용되는 화면이다.


물론 위화면에서 눌러준 것과 별개로 각 UIViewController별로 허용 화면을 설정해 줄 수 있다.

- (NSUInteger) supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskPortrait//세로 화면만 허용

    //return UIInterfaceOrientationMaskAll //전체 화면 허용 

    //return UIInterfaceOrientationMaskPortraitUpsideDown //거꾸로만 허용

    //return UIInterfaceOrientationMaskLandscape; //가로화면만 허용

}


위의 코드와 같이 각 뷰별로 허용 화면을 설정하면 되나 이 때 주의 점이 있다.

화면 전환시 코드보다 프로젝트 요약화면(위의 스샷)에서 눌러준 허용화면을 우선시 한다. 즉 코드로 허용 화면을 설정할 땐 요약화면에서 허용된 화면 안에서만 조작이 가능하다.


예를 들어 위 스샷처럼 세로, 가로 좌우 만 허용된 상황에선 supportedInterfaceOrientations메소드가 UIInterfaceOrientationMaskAll을 리턴하여도 거꾸로된 화면은 허용이 안된다.


만약에 위의 스샷과 같이 화면이 허용된 상황에서 강제로 UIInterfaceOrientationMaskPortraitUpsideDown (거꾸로 화면만 허용)을 리턴하도록 코드를 짜면

'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'

라는 에러가 발생한다.


기타 Autorotate 관련하여 트러블슈팅 시 도움될 포스팅에 대한 링크.

http://devnote2.tistory.com/entry/iOS6-shouldAutorotate-NavigationViewController-%EB%AC%B8%EC%A0%9C