내가 가지고 UILabel
UICollectionViewCell 내부의 레이블에 제약 조건을 추가하는 방법은 무엇입니까?
var cellTitle = UILabel()
내가 식별자와 셀을 정의하라 cellForItemAtIndexPath
방법이 있고 나는이 cellTitle
이름을 만든 라벨에 제약 조건을 추가
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
cellTitle = UILabel(frame: CGRectMake(0, 0, cell.bounds.size.width, 160))
cellTitle.numberOfLines = 3
cell.contentView.addSubview(cellTitle)
cell.addConstraints([
NSLayoutConstraint(item: cell, attribute: .CenterX, relatedBy: .Equal, toItem: cellTitle, attribute: .CenterX, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: cell, attribute: .CenterY, relatedBy: .Equal, toItem: cellTitle, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
])
}
내가이 오류 때 얻을 시뮬레이터를 실행합니다.
아마도 다음 목록에있는 제약 조건 중 적어도 하나는 원치 않는 것일 수 있습니다. 이것을 시도하십시오 : (1) 각 제약 조건을보고 당신이 예상하지 못한 것을 알아 내려고 시도하십시오; (2) 원하지 않는 제약 조건 또는 제약 조건을 추가 한 코드를 찾아 수정하십시오.
하는 제약 조건을
을 파괴함으로써 복구하려고합니다 ("<NSLayoutConstraint:0x7baaac40 UICollectionViewCell:0x7b981570.centerY == UILabel:0x7baa3dd0'Towed by smaller boats, c...'.centerY>",
"<NSAutoresizingMaskLayoutConstraint:0x7baa5c70 h=--& v=--& UILabel:0x7baa3dd0'Towed by smaller boats, c...'.midY == + 80>",
"<NSLayoutConstraint:0x7baa5d50 'UIView-Encapsulated-Layout-Height' V:[UICollectionViewCell:0x7b981570(210)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7bab1110 h=--- v=--- 'UIView-Encapsulated-Layout-Top' V:[UICollectionViewCell:0x7b981570]-(0)-|>"
를) (참고 : 당신이 이해하지 못하는 NSAutoresizingMaskLayoutConstraints을보고있는 경우, UIView의 속성 translatesAutoresizingMaskIntoConstraints에 대한 설명서를 참조하십시오)
메서드를 사용하는 부분에서 셀에 레이블을 가운데에 정렬하려고합니다. 어떤 이유로 라벨은 셀의 중심되는 것을되지 않습니다
나는이 문제를 해결하기 위해 무엇을 할 수 있는가? 나는 모든 지원에 감사드립니다.
이 그렇지 않은 @milesper 레이블의 텍스트 정렬 센터 – milesper
있는지 확인합니다. 텍스트 정렬을 어떻게 가운데에 배치 할 수 있습니까? –