를 사용하여 동적 레이블을 만들 수 있습니다, 나는 3 행으로 배열하려면, 모든 행이 정렬을 선도했으며, 각 행의 마지막 항목은 나머지에 들어갈 수없는 경우 부모보기 공간, 그것은 다음 줄로 이동해야합니다. UIStackview를 사용하면 어떻게 할 수 있습니까?어떻게 내가 다른 크기 10 UILabels이 UIStackView
0
A
답변
0
당신은 그런 식으로 뭔가를 시도 할 수 있습니다 :
귀하의 ViewController에게
for item in array {
let someView = VPView(frame: CGRect(x: 0, y: 0, width: 70, height: 20))
someView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(someView)
}
의 UIView 클래스 :
이class VPView: UIView {
let myLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
self.addLabel()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func addLabels() {
//Mark: - Styles
let labelH: CGFloat = 15
let labelW: CGFloat = 70
//MARK: - My Label
self.myLabel.frame = CGRect(x: 0, y: 0, width: labelW, height: labelH)
self.myLabel.backgroundColor = UIColor.blue
self.myLabel.textAlignment = NSTextAlignment.center
self.myLabel.numberOfLines = 0
self.addSubview(self.myLabel)
self.myLabel.translatesAutoresizingMaskIntoConstraints = false
self.myLabel.heightAnchor.constraint(equalToConstant: labelH).isActive = true
self.myLabel.widthAnchor.constraint(equalToConstant: labelW).isActive = true
self.myLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
self.myLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
}
}
I가'UICollectionView'을 사용하는 것이 좋습니다 것 그렇게 쉬운 방법입니다. – the4kman
나는 @의 the4kman로, 아마'UICollectionView'은 당신이 달성하려고하는 것에 더 적합 할 것입니다 동의합니다. the4kman @ –
당신에게 – NabiMo