繁体   English   中英

使用SegmentController使TableView消失并显示UIContainerView

[英]Use SegmentController to Make TableView Disappear and UIContainerView Appear

我正在尝试使用段控制器在tableView和容器视图之间进行切换,但是当我尝试在它们之间进行切换时,只有一半有效。 TableView出现并消失,但是容器视图从不出现。

这是我的代码:

@IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        profileTableView.isHidden = false
        modelsContainerView.isHidden = true
    } else {
        profileTableView.isHidden = true
        modelsContainerView.isHidden = false
    }
}

更新

如果我使用此代码,则模拟工作正常。 出现容器视图,但它不像tableview那样填满屏幕。

    @IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 1
            self.modelsContainerView.alpha = 0
        })
    } else {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 0
            self.modelsContainerView.alpha = 1
        })
    }
}

更新图片

我可以说它不起作用,因为我已将容器视图的背景色设置为粉红色。 这是当我尝试从TableView(可工作)切换到容器View时的样子: 使用TableView进行细分

应显示容器视图的细分(无效)

故事板

所有插座似乎都已连接。 我的UI设置是段控制器后面的绿色视图,下面是tableView,而containerView是在同一位置。

非常感谢您对高级的帮助。

试试这个方法...

在此处输入图片说明

Seg背景视图的高度为45点,固定的顶部,顶部,底部全部等于0

配置文件容器固定为等于0前导,尾随,底部,而顶部固定为Seg Background的底部。

但是您看不到配置文件容器(红色背景),因为模型容器(橙色背景)位于其顶部,并且...

“模型容器”的宽度和高度相等,并且在水平和垂直方向上居中,均以“轮廓容器”为准。

配置文件容器中嵌入了配置文件表VC。

模型容器中嵌入了模型VC。

这个想法是:

选择“ Seg 0 ,“轮廓容器”为alpha 1 且未隐藏,而“模型容器”为alpha 0且隐藏。

Seg 1被选择时,简介集装箱为α0和隐藏的,而模型Container是α1和隐藏。

class SegContainerViewController: UIViewController {

    @IBOutlet weak var profileContainerView: UIView!
    @IBOutlet weak var modelsContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // start with Profile visible
        // so hide Models and set its alphs to 0
        self.modelsContainerView.alpha = 0
        self.modelsContainerView.isHidden = true

    }

    @IBAction func switchAction(_ sender: UISegmentedControl) {

        // on segment select, the "other" container will be
        // transparent and hidden, so
        // un-hide it, then animate the alpha for both (for cross-fade)
        // on animation completion, hide the now transparent container

        if sender.selectedSegmentIndex == 0 {

            self.profileContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 1
                self.modelsContainerView.alpha = 0

            }, completion: { (finished: Bool) in

                self.modelsContainerView.isHidden = true

            })

        } else {

            self.modelsContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 0
                self.modelsContainerView.alpha = 1

            }, completion: { (finished: Bool) in

                self.profileContainerView.isHidden = true

            })

        }

    }
}

编辑:

要访问Embedded View Controller,请重写prepareForSegue:

var theProfileVC: ProfileTableViewController?
var theModelsVC: ModelsViewControler?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let vc = segue.destination as? ProfileTableViewController {

        // do something here if desired, like setting a property of the VC

        // save a reference so we can use it later
        theProfileVC = vc
    }

    if let vc = segue.destination as? ModelsViewControler {

        // do something here if desired, like setting a property of the VC

        // save a reference so we can use it later
        theModelsVC = vc

    }

}

我还用一个示例更新了GitHub存储库。


如果您想深入研究,我将其作为一个示例项目: https : //github.com/DonMag/SegmentsAndContainers

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM