내 스택보기 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
입니다.
누구나 내 문제를 해결할 수 있기를 바랍니다. 미리 감사드립니다.
? 지금 당장은 '= UIStackView()'를 사용하여 두 스택 뷰를 초기화하는 것처럼 보입니다. 기본적으로 반환되는 프레임은 0.0, 0.0입니다. –
@BenjaminLowry 그는 제약 조건을 사용하고 있습니다. – matt
@matt 내가 틀렸다면 나에게 맞는 것이지만, 제약 조건이 생성되기 전에 스택보기의 값을 출력하지 않는가? –