2016-08-28 2 views
1

내 스택보기 autolayout 및 해당 arrangedSubView (2 레이블 및 1 이미지) 코드에서 만들려고 해요,하지만 한 스택보기 집합을 시뮬레이터에 나타납니다. 콘솔에서 결과를 인쇄 할 때 모든 스택 뷰의 프레임이 0.0;0.0 인 경우를 제외하고 모든 레이블과 이미지에 값이 있습니다. 왜 이렇게 된거야? 스택보기가 나타나지 않는 이유는 무엇입니까? 당신이 볼 수 있듯이왜 스택 뷰의 프레임이 (0.0, 0.0)입니까?

코드

let contentView: UIView = UIView() 
let stackView: UIStackView = UIStackView() 

var singleStack = Array(count: 6, repeatedValue: UIStackView()) 
var timeLabel = Array(count: 6, repeatedValue: UILabel()) 
var iconImage = Array(count: 6, repeatedValue: UIImageView()) 
var tempLabel = Array(count: 6, repeatedValue: UILabel()) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    for index in 0...5 {    
     timeLabel[index].text = "0\(index):00" 
     iconImage[index].image = UIImage(named: "sunny")! 
     tempLabel[index].text = "0\(index)°" 

     singleStack[index].addArrangedSubview(timeLabel[index]) 
     singleStack[index].addArrangedSubview(iconImage[index]) 
     singleStack[index].addArrangedSubview(tempLabel[index]) 

     singleStack[index].translatesAutoresizingMaskIntoConstraints = false 
     singleStack[index].axis = .Vertical 
     singleStack[index].alignment = .Center 
     singleStack[index].distribution = .FillEqually 

     stackView.addArrangedSubview(singleStack[index]) 

     print("\(timeLabel[index].text!)") 
     print("\(iconImage[index].image!)") 
     print("\(tempLabel[index].text!)") 
     print("\(singleStack[index])") 
     print("\(stackView)") 
     } 

    stackView.translatesAutoresizingMaskIntoConstraints = false 
    stackView.axis = .Horizontal 
    stackView.alignment = .Fill 
    stackView.distribution = .FillEqually 

    contentView.backgroundColor = UIColor.lightGrayColor() 

    view.addSubview(contentView) 
    contentView.addSubview(stackView) 

    contentView.translatesAutoresizingMaskIntoConstraints = false 

    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "H:|[stackView]|", 
     options: [], 
     metrics: nil, 
     views: ["stackView": stackView])) 

    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|[stackView]|", 
     options: [], 
     metrics: nil, 
     views: ["stackView": stackView])) 
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "H:|-[contentView]-|", 
     options: [], 
     metrics: nil, 
     views: ["contentView": contentView])) 

    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:[contentView(120)]-16-|", 
     options: [], 
     metrics: nil, 
     views: ["contentView": contentView])) 

} 

콘솔의 인쇄 결과

01:00 
<UIImage: 0x7fb61944f630>, {32, 32} 
01° 
<UIStackView: 0x7fb6194462c0; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fb619445e70>> 
<UIStackView: 0x7fb619443b50; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fb6194433b0>> 

(단지 예를 들어, 하나 개의 조각을 사용) : 아래에있는 내 코드를 참조하십시오 모든 레이블과 이미지에는 값이 있지만 모든 스택 뷰의 프레임은 0.0;0.0입니다.

누구나 내 문제를 해결할 수 있기를 바랍니다. 미리 감사드립니다.

+0

? 지금 당장은 '= UIStackView()'를 사용하여 두 스택 뷰를 초기화하는 것처럼 보입니다. 기본적으로 반환되는 프레임은 0.0, 0.0입니다. –

+0

@BenjaminLowry 그는 제약 조건을 사용하고 있습니다. – matt

+0

@matt 내가 틀렸다면 나에게 맞는 것이지만, 제약 조건이 생성되기 전에 스택보기의 값을 출력하지 않는가? –

답변

1

Array(count: , repeatedValue:)으로 배열을 초기화하고 repeatedValue을 클래스로 사용하면 클래스 카운트 시간의 동일한 인스턴스를 가리키는 포인터 배열을 얻게됩니다. 신속한 수업은 참고로 전달됩니다. 그래서 singleStack[0] === singleStack[2] 어느 === singleStack[5] 등. 이것은 하나의 스택보기가 나타나는 이유입니다. 당신이 절대적으로 당신이 당신의 스택 뷰에 추가하고 arrangedSubviews를 사용하여 어설픈 될 것이라고 전망의 배열을 필요로하지 않는

for index in 0...5 { 
    let singleStack = UIStackView() 
    let timeLabel = UILabel() 
    let iconImage = UIImageView() 
    let tempLabel = UILabel() 

    timeLabel.text = "0\(index):00" 

    iconImage.image = UIImage(named: "sunny")! 

    tempLabel.text = "0\(index)°" 

    singleStack.addArrangedSubview(timeLabel) 
    singleStack.addArrangedSubview(iconImage) 
    singleStack.addArrangedSubview(tempLabel) 

    singleStack.axis = .Vertical 
    singleStack.alignment = .Center 
    singleStack.distribution = .FillEqually 
    self.stackView.addArrangedSubview(singleStack) 
} 

: 당신은 같은에 루프 당신을 변경해야합니다. 이 경우는 사용을 같은 초기화 블록을 가지고 더 나은 것 :

let singleStack: [UIStackView] = 
{ 
    var singleStacks = [UIStackView]() 
    for _ in 0...5 
    { 
     singleStacks.append(UIStackView()) 
    } 
    return singleStacks 
}() 

등 ... 당신은 "stackViews"프레임이 변경됩니다 기대하고 코드의 어떤 라인

+0

조언을 주셔서 대단히 감사합니다. – stephen

관련 문제