解决方法
当旋转发生时,您需要以编程方式更新
UIStackView
的轴属性.文件说:
The exact layout varies depending on the stack view’s axis,
distribution,alignment,spacing,and other properties.
纯粹使用故事板无法实现这一目标.
工作方案
我创建了一个演示项目并验证它是否有效.我由一个视图控制器组成.
class ViewController: UIViewController { @IBOutlet weak var stackView: UIStackView! override func viewWillTransitionToSize(size: CGSize,withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator) { coordinator.animateAlongsideTransition({ (context) -> Void in let orientation = UIApplication.sharedApplication().statusBarOrientation if orientation.isPortrait { self.stackView.axis = .Horizontal } else { self.stackView.axis = .Vertical } },completion: nil) } }
stackView是在故事板中创建的UIStackView,初始轴设置为Horizontal.