2014-12-04 1 views
1

iOS 7에서는 작동하지만 iOS 8에서는 작동하지 않는 자동 레이아웃 코드가 있습니다. 두 가지 버전 간에는 내 코드가 변경되지 않았습니다. 나는 아이폰 OS 8에서 동일한 코드를 실행하면iOS 8 : NSLayoutAttributeCenterX에서 내보기를 가운데 놓지 않는 이유는 무엇입니까? (iOS 7에서 작동)

그러나 iOS 7 layout

가, 다음이 발생합니다

상황은 내가 지금처럼 수직으로 쌓아해야 할 몇 가지 서브 뷰를 포함하는 뷰 컨트롤러를 가지고있다 :

iOS 8 layout

다음 내 제약 조건을 설정하는 코드가 될 때 :

- (void) updateViewConstraints { 
    [super updateViewConstraints]; 

    NSDictionary *viewsDictionary = @{ 
     @"timeLabel": self.timeLabel, 
     @"recordingStateImage": self.recordingStateImage, 
     @"recordButton": self.recordButton, 
     @"spacer1": spacer1, 
     @"spacer2": spacer2, 
     @"superview": self.view 
    }; 

    if (!portraitConstraints) { 
     // 
     // Portrait Layout 
     // 

     portraitConstraints = [[NSMutableArray alloc] initWithArray: 
      [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[timeLabel][spacer1][recordingStateImage][spacer2(==spacer1)][recordButton(100)]-|" 
                options:0 
                metrics:0 
                 views:viewsDictionary]]; 

     [portraitConstraints addObjectsFromArray:@[ 
      [NSLayoutConstraint constraintWithItem:self.timeLabel 
             attribute:NSLayoutAttributeCenterX 
             relatedBy:NSLayoutRelationEqual 
              toItem:self.view 
             attribute:NSLayoutAttributeCenterX 
             multiplier:1.f 
              constant:0.f], 
      [NSLayoutConstraint constraintWithItem:self.recordingStateImage 
             attribute:NSLayoutAttributeCenterX 
             relatedBy:NSLayoutRelationEqual 
              toItem:self.view 
             attribute:NSLayoutAttributeCenterX 
             multiplier:1.f 
              constant:0.f], 
      [NSLayoutConstraint constraintWithItem:self.recordButton 
             attribute:NSLayoutAttributeCenterX 
             relatedBy:NSLayoutRelationEqual 
              toItem:self.view 
             attribute:NSLayoutAttributeCenterX 
             multiplier:1.f 
              constant:0.f] 
     ]]; 
    } 

    [self.view removeConstraints:self.view.constraints]; 
    [self.view addConstraints:portraitConstraints]; 

} 

왜 이런 일이 발생하는지 잘 모르겠습니다. 나는 그래서 다음 질문에서 언급 한 해결 방법을 살펴 보았다 :

그러나 그들은 (대신 뷰 컨트롤러의 뷰의 서브 뷰에 setNeedsLayout를 호출하는) 일을하지 않았습니다.

NSLayoutAttributeCenterX 및 iOS 8의 자동 레이아웃과 관련된 문제가있는 사람은 누구나 문의 해 주시면 감사하겠습니다.

+0

여기에 컨테이너보기가 무엇입니까 ??? –

+0

언제 updateViewConstraints를 호출합니까? viewDidLoad 또는 ViewWillAppear와 같은 라이프 사이클 방식입니까? – kpsharp

+1

updateViewConstraints의 모든 제약 조건을 제거하지 마십시오. XCode에 의해 추가 된 제약 조건도 제거되므로 다시 얻지 못할 것입니다. – Jageen

답변

2



참고 :
그것은 모두 제거합니다 모든 제약 조건을 제거하는 나쁜 관행입니다 XCode에 의해 추가 된 제약 조건으로 장치에보기가 적합합니다.

보기가 (소형 일반보기에서) 가로 및 세로 다른 제약 조건을 가진 경우
가로 및 세로 모드에서 서로 다른 UI를 가졌어요.
크기 클래스 introduce in XCode6backward comparability을 사용할 수 있습니다.

크기 클래스를 매우 잘 설명하는 blog을 따르십시오.
download project 위의 블로그에서 작성하십시오.

출력 : 세로 모드 enter image description here

보기 가로 모드

보기 : enter image description here

+0

jw -이 물건을 이전 버전과 호환되게 만드는 것에 대한 생각이 있습니까? 내가 끝낸 것은 내 오래된 논리를''updateConstraints'''에 보관하는 것이지만 OS가 linusthe3rd

+0

은 이전 버전과 호환됩니다 https://developer.apple.com/ 라이브러리/ios/recipes/xcode_help-IB_adaptive_sizes/chapter/DeployingSizeClassesonEarlieriOSVersions.html # // apple_ref/doc/uid/TP40014436-CH13-SW1 – Jageen

+0

나는이 질문을 내게 시도하지는 않았다. 내 도움 http://stackoverflow.com/questions/24172860/ios-7-and-ios-6와 호환 가능한 how-can-xcode-6-adaptisi-be-backwards 호환 – Jageen

0

iOS 8과 Xcode 6은 요소를 배치하는 데 도움이되는 추가 제약 조건을 뷰에 추가합니다. 나는 이것을 Jageen 위와 comment on a github issue에 의해 계산했다.

iOS 8에서 [self.view removeConstraints]을 호출하지 않는 것이 이러한 추가적인 필수 제약 조건을 제거하므로 문제를 해결할 수 있습니다. 이에 대한 내 개인적인 해결책은 iOS 8의 오리엔테이션 관련 제약 조건을 위해 펜촉을 활용하고 위의 코드를 버전 7.0에서만 실행하도록 OS 확인을 수행하는 것입니다.

누구나 역 호환 가능한 해결 방법이있는 경우 이 문제와 관련하여보기에서 removeConstraints을 호출하면보기에 매우 흥미가 있습니다 (세로와 가로가 다른 레이아웃이므로). 문제 및 영업 이익은 내가 답변으로 코멘트를 게시 동일한 문제에 대한 자세한 정보가 필요 해결하기 위해 내 댓글 도움으로

+0

세로 및 가로 레이아웃이 다르기 때문에 크기 클래스 (XCode6에서 역 비교 가능)를 사용할 수 있습니다. http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial – Jageen

0

나는이 문제를 가지고 있었다. 내 경우에는 그 원인이 라인이었다.

self.view.translatesAutoresizingMaskIntoConstraints = NO; (self는 UIViewController)

제약 조건을 설정 한 후 콘솔에 중단 점 중에 po self.view.layer를 입력하면 정상적으로 나타납니다. 나는 viewDidLoad (viewWillAppear과 viewWillLayoutSubviews를 실험하여 같은 결과를 얻는다)에서 제약 조건을 설정했다. 그러나 viewDidAppear에 중단 점을 추가하고 콘솔에서 동일한 명령을 입력하면 self.view의 너비와 높이가 모두 0으로 변경되었습니다. 나는 위의 줄을 제거하고 작동했습니다. self.view는 높이와 너비를 유지하고 서브 뷰는 중앙에 정렬되었습니다.

관련 문제