翻转 UIStackView 中项目的位置 [英] Flipping the positions of items in a UIStackView

查看:23
本文介绍了翻转 UIStackView 中项目的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIStackView,它是在 Interface Builder 中创建的,它包含两个视图.现在我正在尝试以编程方式更改这些视图的顺序(可以说是交换它们的位置).

I have a UIStackView, created in Interface Builder that contains two views. Now I'm trying to programatically change the order of these views (exchange their positions, so to speak).

是否有一个简单的解决方法,使用 Interface Builder 来完成这个?

Is there an easy fix for this that DOES NOT use Interface Builder to accomplish this?

谢谢:)

推荐答案

UIStackView 中创建具有 2 个 UIViews 的布局,如下所示:

Create a layout with 2 UIViews in a UIStackView like so:

为您的 UIStackView 创建一个出口到您的 UIViewController.在 viewDidLoad 中对您的 UIStackView 调用 addArrangedSubview 以更改顺序.请参阅下面的片段.

Create an outlet for your UIStackView to your UIViewController. Call addArrangedSubview on your UIStackView in viewDidLoad to change the order. See snippet below.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var stackView: UIStackView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Switches order of the stackView
        stackView.addArrangedSubview(self.stackView.subviews[0])
    }
}

这应该是结果:

addArrangedSubview 获取给定的视图并将其添加到排列的子视图数组的末尾.在我们的例子中,我们将 index[0] 处的红色视图添加到 UIStackView 的末尾(右侧).

The addArrangedSubview takes the given view and adds it to the end of the arrangedSubviews array. In our case we take the red view at index[0] and add it on the end (right) of the UIStackView.

这篇关于翻转 UIStackView 中项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆