我有几个UIStackView
我以编程方式添加,我希望他们在应用说Axis
时有另一个Regular Width and Any Height
。
这甚至可能,就像它在界面构建器中一样吗?
我在Google上遇到的只是willTransitionToTraitCollection
,但不是我需要的。
为了更容易理解我需要的东西:
for i in numberOfItems {
let stackView = UIStackView()
stackView.append... // add views in this new stack view
// here is where I need help:
stackView.axis = horizontal
stackView.addSomething... stackView.axis = vertical if regular width and any height.
existingStackView.append.... stackView
}
修改
我找到了另一种方法,但是当我旋转它时,我在iPhone 6+上遇到了一些奇怪的行为。 Git回购:https://github.com/bjaskj/iOSStackViewConstraints
class MyStackView: UIStackView {
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Regular {
axis = UILayoutConstraintAxis.Horizontal
} else {
axis = UILayoutConstraintAxis.Vertical
}
}
}
的ViewController:
@IBOutlet weak var mainStackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
addElement("Name", description: "Saint Petersburg")
addElement("Native name", description: "Санкт-Петербург")
addElement("Country", description: "Russia")
addElement("Federal district", description: "Northwestern")
addElement("Economic region", description: "Northwestern")
addElement("Established", description: "May 27, 1703")
}
func addElement(title:String, description:String) {
let labelTitle = UILabel()
labelTitle.backgroundColor = UIColor.redColor()
labelTitle.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
labelTitle.text = title
labelTitle.textColor = UIColor.whiteColor()
let labelDescription = UILabel()
labelDescription.backgroundColor = UIColor.blueColor()
labelDescription.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
labelDescription.text = description
labelDescription.textColor = UIColor.whiteColor()
let stackHolder = MyStackView()
stackHolder.axis = .Vertical
stackHolder.distribution = .FillEqually
stackHolder.addArrangedSubview(labelTitle)
stackHolder.addArrangedSubview(labelDescription)
stackHolder.spacing = 8
mainStackView.addArrangedSubview(stackHolder)
}
答案 0 :(得分:5)
<强>更新强>
好的,所以看起来你有两个问题:
1)当我将stackviews添加到场景中时,如何根据大小类添加适当的stackview.axis:
for i in numberOFItems {
let stackView = UIStackView()
if view.traitCollection.verticalSizeClass == .Unspecified && view.traitCollection.horizontalSizeClass == .Regular {
stackView.axis = .Vertical
} else {
stackView.axis = .Horizonal
}
stackView.addArrangedSubview(arrayOfViews[i])
}
2)当手机方向改变时,如何使堆栈视图从水平轴变为垂直轴:
您需要拥有一组已初始化的堆栈视图。然后,您需要逐步完成委托方法中的stackviews并更改其方向。
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
var axis
if view.traitCollection.verticalSizeClass == .Compact {
axis = UILayoutConstraintAxis.Horizontal
} else {
axis = UILayoutConstraintAxis.Vertical
}
for stackView in stackViewArray {
stackView.axis = axis
}
}
视图有一个traitCollection,包括verticalSizeClass和horizontalSizeClass。它们的值可以是.Compact,.Regular和.Unspecified。
方法traitCollectionDidChange应该在修改尺寸等级时(即手机转动时)告诉您。
如果仍然无法解答您的问题,请尝试重写您的问题,以便更容易理解
另外,我没有办法知道设置这些属性一次并完成它,你必须处理这两种情况。