4

반투명 탐색 막대와 상태 표시 줄을 사용하고 있으며 내 View Controller는 전체 화면을 원합니다. 따라서 내 View Controller의보기는 탐색 및 상태 표시 줄 아래로 확장되며 화면의 전체 크기를 차지합니다.실제로 회전하기 전에 내비게이션 막대의 새로운 프레임을 결정하십시오 - iOS

또한 탐색 표시 줄 아래에 직접 정렬하려는 레이블이 있습니다. Label과 Navigation Bar 사이에 제약 조건을 직접 추가 할 수 없기 때문에 Label의 Top과 Top의 상위 뷰 사이에 제약 조건을 추가합니다. contstraint의 상수를 Status Bar의 높이 + Navigation Bar의 높이와 같게 설정했습니다.

내비게이션 막대의 높이가 변경되고 내 레이블이 잘 회전해야하기 때문에 세로와 가로 사이의 회전 중에 문제가 발생하므로 willRotateToInterfaceOrientation:의 탐색 모음의 새 높이를 알아야합니다. 방법.

View Controller를 세로 또는 가로 방향으로 탐색 할 때 Label이 정확한 위치에 있는지 확인하기 위해이 방법을 사용합니다. 그것은 잘 작동합니다.

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // Get Y origin for Label 
    CGFloat navBarY = self.navigationController.navigationBar.frame.origin.y; 
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height; 
    CGFloat labelY = navBarY + navBarHeight; 

    // Set vertical space constraint for Label 
    self.labelConstraint.constant = labelY; 
} 

탐색 막대의 높이가 44px에서 32px로 바뀌어 방향이 변경 될 때이 방법을 사용하여 레이블의 위치를 ​​변경합니다. 문제는 회전이 실제로 발생하기 전에 탐색 바가 회전 한 후 NEW 높이를 가져와야한다는 것입니다.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    // Get Y origin for Label 
    CGFloat navBarY = self.navigationController.navigationBar.frame.origin.y; 
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height; 
    CGFloat labelY = navBarY + navBarHeight; 


    // This obviously returns the the current Y origin for label before the rotation 
    // Which isn't very useful. 

    NSLog(@"labelY: %f", labelY); 


    // This code produces the desired effect, but obviously I want to avoid 
    // hard-coding the values. 

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
     self.labelConstraint.constant = 20 + 32; 
    } else { 
     self.labelConstraint.constant = 20 + 44; 
    } 
} 

재미를 위해, 나는 didRotateFromInterfaceOrientation:에서 회전 후 라벨의 Y 원점을 설정하려고했으나 예상대로 부드럽게 아니에요 및 회전이 완료된 후 라벨이 제자리에 고정.

미리 도움을 주셔서 감사합니다.

+0

이 문제에 대한 답변을 찾았습니까? –

+0

동일한 문제가 발생합니다 ... 어떤 해결책입니까? –

답변

0

대답은 다음과 같습니다. 크기 클래스. 당신은 그것에 대해 스스로에게 알릴 수 있습니다, 그것은 Apple에 의해 잘 문서화되어 있습니다. 이 기능을 사용해야합니다 : func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator). 보기를 설정할 때보기 컨트롤러에 두 개의 속성을 추가하는 것이 좋습니다. 두 배열 모두 세로보기 용, 가로보기 용으로 모두 NSLayoutConstraint의 배열입니다. iOS 8부터 한 번에 여러 제약 조건을 활성화/비활성화 할 수 있습니다. 위에서 언급 한 기능에서 다음을 수행하십시오.

if newCollection.verticalSizeClass == .Compact { //orientiation is landscape 
    NSLayoutConstraint.deactivateConstraints(self.portraitConstraints) 
    NSLayoutConstraint.activateConstraints(self.landscapeConstraints) 
} else { 
    NSLayoutConstraint.deactivateConstraints(self.landscapeConstraints) 
    NSLayoutConstraint.activateConstraints(self.portraitConstraints) 
} 

먼저 비활성화해야합니다. 그렇지 않으면 제약 조건이 충돌합니다.

"swifty"답변을 작성해 주셔서 감사합니다. ObjC로 '번역'할 수있을 것입니다. 질문이 있으시면 언제든지 물어보십시오.