7

我有一些UIStackView我以编程方式添加的,我希望它们Axis在应用程序处于 say 时有另一个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
        }
    }
}

视图控制器:

@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)
}

当我旋转时会发生什么iPhone 6+在此处输入图像描述

但是,如果我以旋转位置启动应用程序,我期望并得到什么: 在此处输入图像描述

4

1 回答 1

7

更新:

好的,看来您有两个问题:

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)当手机方向改变时,如何使stackview从水平轴变为垂直轴:

您将需要一个已初始化的堆栈视图数组。然后,您需要单步执行委托方法中的堆栈视图并更改它们的方向。

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 和horizo​​ntalSizeClass。它们的值可以是 .Compact、.Regular 和 .Unspecified。

traitCollectionDidChange 方法应该告诉您何时修改尺寸类(即当手机转动时)。

如果这仍然不能回答您的问题,请尝试重写您的问题,以便更容易理解

此外,我不知道设置这些属性一次并完成它,你必须照顾这两种情况。

于 2015-09-17T18:58:50.790 回答