코드에서 topLayoutGuide 및 bottomLayoutGuide에 대한 자동 레이아웃 제약 조건 만들기
뷰와 레이아웃 가이드 중 하나 사이에 자동 레이아웃 제약 조건을 만드는 방법에 대한 Apple의 설명서 는 VFL을 사용한 예제 만 보여줍니다 .
VFL 없이 프로그래밍 방식으로 이러한 제약 조건을 만들 수있는 방법이 있습니까 ( NSLayoutConstraint
의 다른 API 또는 유사한 사용)?
(참고 : 인터페이스 빌더가 아닌 코드에서이 작업을 수행하는 것에 대해 구체적으로 묻고 있습니다. length
가이드 세트를 제약 조건에 대한 정적 상수로 계산 하는 것을 원하지 않고 레이아웃 가이드 길이가 변경되는 제약 조건을 원합니다. 제한된 뷰가 자동으로 위치를 조정합니다.)
들어 UIButton
당신이 아래에 20 점을 배치 할 것을 UIViewController.topLayoutGuide
당신은 만들 NSLayoutConstraint
과 같이 :
[NSLayoutConstraint constraintWithItem:self.button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:20.0];
iOS 9에서는 NSLayoutConstraint
다음 과 같이 만들 수도 있습니다 .
[self.button.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor
constant:20.0];
@JamieMcDaniel의 답변을 보완하기 위해 Swift + iOS9 버전은 다음과 같습니다.
self.button.topAnchor
.constraintEqualToAnchor( self.topLayoutGuide.bottomAnchor ).active = true
.active = true
그렇지 않으면 구속이 자동으로 시작되지 않으므로 부품을 잊지 마십시오 .
이것은 내가 만든 요점 이며, 모든 하위 뷰를 xib에 추가 된 탱크 뷰 (컨테이너 뷰)에 포함해야하며, 탱크 뷰-슈퍼 뷰 xib 제약 조건을 제거하고 iOS6 모양을 제공하는 topLayoutGuide에 상위 제약 조건을 추가합니다. 당신이 성취하고 싶은 것에 흥미로울 수 있습니다.
//This should be added before the layout of the view
- (void) adaptToTopLayoutGuide {
//Check if we can get the top layoutguide
if (![self respondsToSelector:@selector(topLayoutGuide)]) {
return;
}
//tankView is a contaner view
NSArray * array = [self.tankView referencingConstraintsInSuperviews]; //<--For this method get the Autolayout Demistified Book Sample made by Erica Sadun
[self.view removeConstraints:array];
NSArray * constraintsVertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide]-0-[tankView]|" options:0 metrics:nil views:@{@"tankView": self.tankView, @"topLayoutGuide":self.topLayoutGuide}];
[self.view addConstraints:constraintsVertical];
NSArray * constraintsHorizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tankView]|" options:0 metrics:nil views:@{@"tankView": self.tankView}];
[self.view addConstraints:constraintsHorizontal];
}
@Jamie McDaniel에 추가하여 즉시 명확하지 않은 경우 생성하도록 제안한 제약 조건을 추가해야합니다.
NSLayoutConstraint *buttonTopConstraint = [NSLayoutConstraint constraintWithItem:self.button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:20.0];
[self.view addConstraint:buttonTopConstraint];
'Development Tip' 카테고리의 다른 글
조각의 onSaveInstanceState ()는 호출되지 않습니다. (0) | 2020.11.21 |
---|---|
Intent.ACTION_GET_CONTENT와 Intent.ACTION_PICK의 차이점 (0) | 2020.11.21 |
SQL에서 날짜를 비교하는 쿼리 (0) | 2020.11.21 |
Vue 프로젝트에서보기와 구성 요소 폴더의 차이점은 무엇입니까? (0) | 2020.11.21 |
모두 없음보다 큰가요? (0) | 2020.11.21 |