Swift : 프로그래밍 방식으로 하위보기에 대한 자동 레이아웃 제약 조건을 설정해도보기 크기가 조정되지 않습니다.

FrankZp

다음 설정이 있습니다. UIView는 프로그래밍 방식으로 여러 하위 뷰 (UILabels)를 추가하고 자동 레이아웃 제약 조건도 설정하여 레이블 사이와 UIViews 가장자리 사이의 거리가 각각 10이되도록합니다. 목표는 UIView가 공백을 포함한 모든 하위보기 (동적 텍스트가있는 레이블)의 내용에 따라 크기를 설정하는 것입니다.

다음 코드를 사용하지만 작동하지 않는 것 같습니다. UIView는 크기가 조정되지 않고 레이블이 축소됩니다.

// setup of labelList somewhere else, containing the label data
var lastItemLabel: UILabel? = nil
var i = 1
for item in itemList {
   let theLabel = UILabel()
   // ... label setup with text, fontsize and color
   myView.addSubview(theLabel)
   theLabel.translatesAutoresizingMaskIntoConstraints = false

   // If it is the second or more 
   if let lastLabel = lastItemLabel {
        theLabel.leadingAnchor.constraint(equalTo: lastLabel.trailingAnchor, constant: 12).isActive = true
        theLabel.topAnchor.constraint(equalTo: myView.topAnchor, constant: 10).isActive = true
        // if it is the last label
        if i == labelList.count {
            theLabel.trailingAnchor.constraint(equalTo: myView.trailingAnchor, constant: 12).isActive = true
        }
   }
   // If it is the first label
   else {
       theLabel.leadingAnchor.constraint(equalTo: myView.leadingAnchor, constant: 12).isActive = true
       theLabel.topAnchor.constraint(equalTo: myView.topAnchor, constant: 10).isActive = true
   }

   lastItemLabel = theLabel
   i += 1
}
Paulw11

콘텐츠가 장치의 실제 디스플레이보다 커야하므로 UIScrollView레이블을 포함하려면를 추가해야합니다 .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관