2016-12-21 1 views
1

내가 collectionview cell나는 프로그래밍 방식

class CollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var label: UILabel! 
    override func layoutSubviews() { 
    super.layoutSubviews() 
     constraint() 
    } 
    func constraint() { 

     label.addConstraint(NSLayoutConstraint(item:label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 25)) 
    } 
} 

의 레이블 설치 높이 제한에 노력하고 collectionview 셀에 위의 라벨 제약의 높이를 않았다 설정과 작동하지 않습니다. 여기서 layoutSubviews 선언이 작동합니까? 이 레이아웃에 영향을 미치지 않는 경우에도

답변

2

제약 조건을 사용하고 코드에서 변경하는 편리한 방법이 있습니다. XIB 또는 Stroyboard에 결박

@IBOutlet weak var labelHeight: NSLayoutConstraint! 

둘째, :

첫째, 제약 조건 속성을 선언 enter image description here

마지막으로, 당신이 프로그래밍 방식으로 변경할 수 있습니다 :

self.labelHeight.constant = 130 
+0

collectionview를 사용하고 있는데 "콘센트가 반복되는 콘텐트에 연결될 수 없다"고했기 때문에 할 수 없습니다. – leaner122

+0

@ leaner122, 레이블은 CollectionViewCell에 있으므로 바인딩은 CollectionviewCell CLASS의 속성과 CollectionviewCell의 Label 제약 사이에 있어야합니다 XIB 또는 Stroryborad. –

0
NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: label, attribute:.Height, multiplier: 1.0, constant:25.0) 

또는

NSLayoutConstraint.constraintsWithVisualFormat(@"V:[label(==24)]", options: nil , metrics: nil, views: NSDictionaryOfVariableBindings(label)) 

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

(...) 각 매개 변수에 대한 값을 지정해야합니다

. 최종 결과는 상당한 양의 상용구 코드이며, 일반적으로 읽기가 더 어렵습니다. (...)