2015-09-17 34 views
1

我有几个UIStackView,我以编程方式添加,我希望他们有一个其他Axis当应用程序在说Regular Width and Any Height如何通过代码在方向更改上添加对UIStackView.Axis的更改?

这甚至可能,就像它在界面生成器?

我所碰到的谷歌是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) 
} 

什么,当我旋转iPhone 6+发生的情况是这样的: enter image description here

但我希望和得到,如果我开始旋转位置的应用程序是什么: enter image description here

+0

您是否尝试更换轴?顺便说一句。一切可能的事情都可以在代码中完成。除了watchOS开发。 – dasdom

+0

更改轴的工作是,但我正在寻找的是当我创建这些'UIStackViews',他们有这添加到它,而不是我必须依靠视图控制器事件来获得通知,然后循环通过我堆栈适应。 – Bjarte

回答

0

更新:

好了,好像你有两个问题:

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变化:

你娘家有你已经初始化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 
    } 
} 

该视图具有包含verticalSizeClass和horizo​​ntalSizeClass的traitCollection。这些可以具有.Compact,.Regular和.Unspecified的值。

的方法traitCollectionDidChange应该告诉你,每当大小类被修改(即当电话接通)。

如果这仍然没有回答你的问题,请尝试重写你的问题,以便更容易理解

此外,也没有办法,我知道一旦设置这些属性,并用它做,你必须照顾这两种情况。

+0

我不确定你是否正确理解我,是否可以在我的for循环中做到这一点,而不是在UIViewController/UIView上做这个,然后对我的子视图做一个for循环?或者制作一个继承UIStackView的自定义类,并针对每种可能需要的情况重写它。 Interface Builder如何做到这一点?他们是否添加了一些魔术代码,通过UIViewController或UIView上的traitCollectionDidChange设置它,当你将它连接到故事板或XIB? – Bjarte

+0

嗨@Bjarte,我更新了我的答案。只有一些全局变量,你可以检查,告诉你的大小类。干杯! –

+0

那么当我在代码中旋转设备时,如何更改轴,这些代码是我在“//这里是我需要帮助的地方”发布的代码?我想要执行一次添加操作。 – Bjarte