2013-06-12 3 views
2

사이에 NSLayoutConstraint을 추가하고 self.view 사이에보기를 추가 할 수 있습니까? 여기에 자기는 UIViewController 인스턴스이며 _textField는 내가 필요하면 UI가 navigationBar이 반투명인지 여부 모두에 관계없이보고해야한다는 것입니다 self.view탐색 막대와 뷰 컨트롤러 간 NSLayoutConstraint보기

의 하위 뷰입니다.

나는 다음을 시도했다. 그러나 그것은 작동하지 않습니다.

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:_textField 
                 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
                 toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom 
                multiplier:1.0 constant:20]; 
[self.navigationcontroller.view addConstraint:cn]; 
+0

콘솔에 오류 메시지가 있습니까? –

+0

@Sj., 혹시 이걸 알아 냈어? 동일한 문제가 여기에서 발생하여 모든 것을 검색합니다. – Pat

+0

@ Pat 얼마간 시간을 보낸 후, 나는 그 접근법을 떨어 뜨렸다. 해결책이 있으면 찾으십시오. 감사. –

답변

2

textField 상단과 부모보기 상단 사이에 제약 조건을 추가하십시오. 구속 조건의 상수는 상태 막대의 높이 + 탐색 막대의 높이로 설정할 수 있습니다.

분명히 다음 코드 조각은 상태 표시 줄과 탐색 모음이 모두 반투명이고보기 컨트롤러가 전체 화면 레이아웃을 원할 경우에만 작동합니다. 필요한 경우 투명성을 쉽게 테스트하고 그에 따라 조정할 수 있습니다.

인터페이스 빌더를 사용하는 경우 기존 제약 조건에 대해 IBOutlet을 생성하고 새로운 제약 조건을 생성하는 대신 상수로 설정할 수도 있습니다.

// Obtain the view rect of the status bar frame in either portrait or landscape 
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; 
CGRect statusBarWindowRect = [self.view.window convertRect:statusBarFrame fromWindow: nil]; 
CGRect statusBarViewRect = [self.view convertRect:statusBarWindowRect fromView: nil]; 

// Add Status Bar and Navigation Bar heights together 
CGFloat height = self.navigationController.navigationBar.frame.size.height + 
statusBarViewRect.size.height; 

// Create & Add Constraint 
NSLayoutConstraint *constraint = 
[NSLayoutConstraint constraintWithItem:self.fieldLabel 
          attribute:NSLayoutAttributeTop 
          relatedBy:0 
           toItem:self.view 
          attribute:NSLayoutAttributeTop 
          multiplier:1 
           constant:height]; 

[self.view addConstraint:constraint]; 
6

예 이동 막대와보기간에 제약 조건을 추가 할 수 있습니다. 네비게이션 컨트롤러에 추가 된 루트보기 컨트롤러는 topLayoutGuide를 포함합니다. 그래서 다음과 같은 코드를 조정 : 내가 모두의 탐색 컨트롤러하지만 네비게이션 컨트롤러의 rootViewController를 참조하고 있지 않다

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:_textField 
                 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
                 toItem:self.rootViewController.topLayoutGuide attribute:NSLayoutAttributeBottom 
                multiplier:1.0 constant:20]; 
[self.rootViewController.view addConstraint:cn]; 

통지를.

또한 bottomLayoutGuide를 사용하여 동일한 방법으로 TabBar 위로 이동할 수 있습니다. (하지만 여기에 해결 방법 패치가있는 iOS 프레임 워크의 버그로 실행해야 할 경우 : UIViews ending up beneath tab bar)

5

UIViewControllertopLayoutGuide 속성을 확인하십시오. 이렇게되면`의 UIViewController '에 대한 애플의 문서에서 예제가있다

... 다음

topLayoutGuide 
Indicates the highest vertical extent for your onscreen content, for use with Auto Layout constraints. (read-only) 

@property(nonatomic, readonly, retain) id<UILayoutSupport> topLayoutGuide 

그리고 ... 프로그램이 속성을 사용하는 방법의 예로서

자동 레이아웃, 상단 모서리가 상단 레이아웃 가이드보다 20 포인트 아래에 있도록 컨트롤을 배치하려고한다고 가정 해보십시오. 이 시나리오는 위에 나열된 시나리오 중 에 적용됩니다. 다음과 유사한 코드를 사용하십시오.

[button setTranslatesAutoresizingMaskIntoConstraints: NO]; 
id topGuide = myViewController.topLayoutGuide; 
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide); 
[myViewController.view addConstraints: 
    [NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]" 
               options: 0 
               metrics: nil 
                views: viewsDictionary] 
self.view layoutSubviews; // You must call this method here or the system raises an exception 
];