0

사용자 정의 뷰는 라이브러리에서 UIView 객체를 드래그하여 IB에 있으며 클래스 이름은 사용자 정의 뷰 클래스 이름으로 설정됩니다. 이 사용자 정의보기에는 하위보기가 있으며 init?(coder aDecoder: NSCoder)에 추가됩니다.앵커 제약 조건은 하위 뷰를 확대하는 대신 슈퍼 뷰를 축소합니다.

앵커 유형 제약 조건은 어떻게 구성되어야하며, 사용자 정의 뷰에서 _centralLabel 서브 뷰가 중앙 설정되고 크기가 사용자 정의 뷰 크기의 25 %가되도록 위치를 지정해야합니까?

코드가 다음과 같이 기록되는 경우, 즉 대신 _centralLabel의 크기를 설정하는 맞춤형 뷰의 25 %가 될

override func updateConstraints() { 
    if !_subviewsConstraintsAdded { 
     _centralLabel.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.25).isActive = true 
     _centralLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.25).isActive = true 
     _centralLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true 
     _centralLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
     _subviewsConstraintsAdded = true 
    } 
    super.updateConstraints() 
} 

코드의 결과

, 커스텀 뷰에 수축 (0,0)은 updateConstraints()가 호출되는 동안 _centralLabel의 크기입니다.

답변

0

누락 비트였습니다 :

프로그램이 광고와 하위 뷰 인스턴스화 이후 어딘가에 위치해야
_centralLabel.translatesAutoresizingMaskIntoConstraints = false 

다음 _centralLabel의 하위 뷰가 확대되었다이 보정 후의

_centralLabel = UILabel() 

하고 이동 뷰 자체는 원래 크기, 모양 및 위치를 유지하면서 뷰의 중심입니다.

관련 문제