2017-12-04 25 views
0

를 사용하여 동적 레이블을 만들 수 있습니다, 나는 3 행으로 배열하려면, 모든 행이 정렬을 선도했으며, 각 행의 마지막 항목은 나머지에 들어갈 수없는 경우 부모보기 공간, 그것은 다음 줄로 이동해야합니다. UIStackview를 사용하면 어떻게 할 수 있습니까?어떻게 내가 다른 크기 10 UILabels이 UIStackView

here's the image

+5

I가'UICollectionView'을 사용하는 것이 좋습니다 것 그렇게 쉬운 방법입니다. – the4kman

+0

나는 @의 the4kman로, 아마'UICollectionView'은 당신이 달성하려고하는 것에 더 적합 할 것입니다 동의합니다. the4kman @ –

+0

당신에게 – NabiMo

답변

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 

    } 
} 
+0

감사합니다 정말 고마워요,하지만이 방법은 UIStackView 단지 하나의 행에서 그들을 배치하기 때문에, 다음 행으로 항목을 이동 나를 도울 수 있다고 생각합니다. – NabiMo

+0

각 라인 UIStackView을 만들 수 있지만 그것은 좋은 방법이 아니다 생각합니다. UICollectionView를 사용해보십시오. – Vitaly