2012-06-11 5 views
4

방향에 따라보기의 이미지 배경을 변경해야합니다. 이를 위해, 나는 viewWillAppear에 statusBarOrientation 접근 방식을 사용하고 있습니다 :viewWillAppear에서 statusBarOrientation에 대한 값이 잘못되었습니다.

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (UIInterfaceOrientationIsPortrait(currentOrientation)) { 
     NSLog(@"PORTRAIT"); 
    } else if (UIInterfaceOrientationIsLandscape(currentOrientation)) { 
     NSLog(@"LANDSCAPE"); 
    } 
} 

문제는 아이 패드가 가로 모드에서 열린 경우에도 콘솔이 항상 인물을 보여주고 있다는 점이다. viewDidAppear의 코드가 제대로 작동하지만 너무 늦어서 사용자가 이미지 변경을 볼 수 있습니다. 따라서 statusBarOrientation의 올바른 상태는 여전히 viewWillAppear에서 사용할 수 없다고 생각됩니다. 그러나이 코드가 작동해야하는 다른 질문을 읽었습니다.

+0

viewWillTransitionToSize에서 더 이상한 것이 있습니다 : 방향을 쿼리 할 수 ​​있으며 전화를 두 번 (viewDidLoad 위치로) 돌려 놓으면 statusBarOrientation이 올바른 답을 줄 것입니다. viewDidLoad에서 다른 답변을 주었지만 이제 장치가 그 때와 같은 방향입니다! –

답변

12

당신은 응용 프로그램의 현재 방향을 결정하기 위해 statusBarOrientation를 사용해서는 안

int type = [[UIDevice currentDevice] orientation]; 
    if (type == 1) { 
     NSLog(@"portrait default"); 
    }else if(type ==2){ 
     NSLog(@"portrait upside"); 
    }else if(type ==3){ 
     NSLog(@"Landscape right"); 
    }else if(type ==4){ 
     NSLog(@"Landscape left"); 
    } 
+1

좋아, 흥미 롭 ... 나는 상태 바 오리엔테이션 접근법이 UIDevice 하나가 더 신뢰할 만하다는 것을 모든 곳에서 읽었지 만 이것이 매력처럼 작동하고있다! 감사! –

+0

당신은 환영합니다 –

+1

나는 도처에서 찾았습니다 ... interfaceOrientation, statusBar 모두 틀린 방향을 반환했습니다. 그렇다면이 솔루션은 +1을했습니다. – iMeMyself

3

을보십시오. Apple의 문서에 따르면 : http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html

이 속성의 값은 수신자의 상태 표시 줄의 방향을 나타내는 상수입니다. 자세한 내용은 UIInterfaceOrientation을 참조하십시오. 이 속성을 설정하면 전환에 애니메이션을 적용하지 않고 상태 표시 줄을 지정된 방향으로 회전시킵니다. 그러나 응용 프로그램에 회전 가능한 윈도우 내용이있는 경우이 방법을 사용하여 임의로 상태 표시 줄 방향을 설정하면 안됩니다. 장치가 방향을 변경하면이 방법으로 설정된 상태 표시 줄 방향은 변경되지 않습니다.

현재 응용 프로그램의 방향을 얻기 위해 UIViewController에의 interfaceOrientation 속성을 사용하십시오.

+1

Orientation을 얻기 위해 Stackoverflow의 여러 답변에서이 방법을 사용하기 때문에이 방법을 사용했습니다. [[UIDevice currentDevice] orientation] 방식으로 접근했습니다. propertyIrientation 속성을 읽는 것도 효과가있었습니다. –

+0

Apple 문서에 대한 링크는 현재 방향을 결정할 때 사용해서는 안된다는 말은 아닙니다. 왜하지 말아야하는지에 대한 언급을 우리에게 줄 수 있습니까? 사실, Apple의 샘플 코드는 다음을 사용합니다. https://developer.apple.com/library/content/samplecode/AVCam/Listings/Swift_AVCam_CameraViewController_swift.html#//apple_ref/doc/uid/DTS40010112-Swift_AVCam_CameraViewController_swift-DontLinkElementID_15 for Cmd-F statusbarOrientation. 실제로, UIDevice 오리엔테이션은 그것이 당신의 UI의 방향과 다를 수 있으므로, 문서로 판단 할 때, 그것을 피해야한다고 경고합니다. –

+0

일부 장치에서 문제가 발생합니다. 사과 고마워. – Warpzit

0

다음은 장치의 interfaceOrientation를 기록하기위한 유용한 코드입니다 :

NSArray* orientations = @[ @"nothing", @"UIInterfaceOrientationPortrait", @"UIInterfaceOrientationPortraitUpsideDown", @"UIInterfaceOrientationLandscapeLeft", @"UIInterfaceOrientationLandscapeRight”]; 
NSLog(@"Current orientation := %@", orientations[self.interfaceOrientation]); 

희망이 사람을 도와줍니다!

관련 문제