Autolayout tips for iOS
6326 단어 AutoLayoutXcodeiOS
검증 및 테스트 모호 제약
Autolayout 제약조건이 충분하지 않으면 hasAmbiguousLayout 을 사용하여 확인할 수 있습니다.모호한 구속이 설정된 경우 반환 true (구속이 완전히 결정되지 않은 경우 항상 반환 falsehasAmbiguousLayout 모호한 구속이 있는 UIVIew 클래스는 실행
exerciseAmbiguityInLayout()을 통해 구속을 충족하는 범위 내에서 구속을 변경합니다.단, 이것은 무작위 변경 제한이므로 디버깅 등에서 사용하는 것이 가장 좋다.exerciseAmbiguityInLayout()
subviews 사용hasAmbiguousLayout, UIView류 또는 UIVIewController류 단위로 간단한 테스트를 할 수 있다.UIViewExtension
extension UIView {
var isAmbiguousLayout: Bool {
var isAmbiguous = false
let views = [self] + subviews
views.forEach { view in
if view.hasAmbiguousLayout {
isAmbiguous = true
print(view.description)
}
}
return isAmbiguous
}
}
func testAutolayout() {
let viewController = SampleViewController.make()
XCTAssertFalse(viewController.view.isAmbiguousLayout)
}
출력 제약조건
충돌 구속
충돌 구속조건은 콘솔에서 볼 수 있지만
Identifier 충돌 구속조건을 즉시 볼 수 있습니다.storyboardxib에identifier를 설정하는 방법과 코드에 설정하는 두 가지 방법.@IBOutlet weak var constraint: NSLayoutConstraint!
...
constraint.identifier = "constraint1"
다음 제약 조건이 실제로 충돌하면 콘솔에서 다음 오류 로그를 출력합니다.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x6000000846a0 'topConstant1' V:[_UILayoutGuide:0x7f8c07f05f40]-(50)-[UILabel:0x7f8c07c0b4a0'Label'] (active)>",
"<NSLayoutConstraint:0x600000084920 'topConstant2' V:[_UILayoutGuide:0x7f8c07f05f40]-(100)-[UILabel:0x7f8c07c0b4a0'Label'] (active)>"
)
실제 충돌의 제한은 괄호 중의 부분이다.<クラス名:メモリアドレス名 'identifier' レイアウト概要>가 출력됩니다.통상적인 화면에는 여러 개의 구성 요소가 있기 때문에 적극적으로 identifier 를 덧붙이는 것이 좋다고 생각합니다.제약조건 가져오기
UIVIew 클래스가 가지고 있는 constraints 중view가 가지고 있는 제약은 수조에서 얻을 수 있지만, constraintsAffectingLayout(for:) 에서는 수평 방향, 수직 방향의 제약을 지정하여 얻을 수 있다.
브레이크 설정
또한 구속 오류는
UIViewAlertForUnsatisfiableConstraints 기호 브레이크에서 캡처할 수 있습니다.Autolayout Breakpoints에 상세한 기록이 있다.Devicer 명령의
_autolayoutTrace 이 방법은 Objective-C 개인 방법이기 때문에 다리를 정의하면 코드에서도 실행할 수 있습니다.뷰 디버거에서 제약조건 보기
뷰 디버거에서 제약조건을 볼 수도 있습니다.실제 디버깅의 제한을 확인할 수 있기 때문에 자주 사용합니다.
방법은 보기 디버거 모드의 상태에서 이미지의 파란색 단추
Show Constraint 단추를 선택한 다음 대상을 선택하면 됩니다.
실제 화면은 다음과 같다.
제약조건을 선택하면 제약조건에 대한 세부 정보를 볼 수 있으므로 실제로 표시되는 제약조건이 올바른지 확인할 수 있습니다.
Reference
이 문제에 관하여(Autolayout tips for iOS), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/shoheiyokoyama/items/82cdf0940848b1258b58텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)