使用 SegmentController 使 TableView 消失和 UIContainerView 出现 [英] Use SegmentController to Make TableView Disappear and UIContainerView Appear

查看:23
本文介绍了使用 SegmentController 使 TableView 消失和 UIContainerView 出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用段控制器在我的 tableView 和容器视图之间切换,但是当我尝试在它们之间切换时,它只工作了一半.TableView 出现和消失,但容器视图从未出现.

I'm trying to use a segment controller to switch between my tableView and a container view, but when I try to switch between them it only half works. The TableView appears and disappears, but the container view never appears.

这是我的代码:

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

更新

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

If i use this code the simulation sort of works. The container view appears but it doesn't fill the screen like the tableview did.

    @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(有效)切换到容器视图时的样子:

I can tell it's not working because I've set the container view's background color to pink. And this is what it looks like when I try to switch from TableView(which works) to container View:

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

All of the outlets appear to be connected. And my UI set up is a green view behind the segment controller, with a tableView below and a containerView that in the same place.

非常感谢您在高级方面的帮助.

Thank you very much for your help in advanced.

推荐答案

试试这个方法...

Seg 背景视图的高度为 45 pts,顶部、前导、尾部均等于 0.

Seg Background view is 45-pts height, and pinned top, leading, trailing all equal to 0.

Profile Container 固定在前导、尾随、底部都等于 0,顶部固定在 Seg Background 的底部.

Profile Container is pinned leading, trailing, bottom all equal to 0, and the top is pinned to the bottom of Seg Background.

但是你看不到 Profile Container(红色背景),因为 Models Container(橙色背景)在它上面,而且...

But you can't see Profile Container (red background), because Models Container (orange background) is on top of it, and...

Models Container 等宽和高度相等,水平和垂直居中,都为 Profile Container.

Models Container is equal width and height, and centered Horizontally and Vertically, all to Profile Container.

Profile Container 中嵌入了 Profile Table VC.

Profile Container has Profile Table VC embedded in it.

Models Container 中嵌入了 Models VC.

Models Container has Models VC embedded in it.

想法是:

When Seg 0 is selected, Profile Container is alpha 1 and not hidden, while Models Container is alpha 0 and is hidden.

When Seg 0 is selected, Profile Container is alpha 1 and not hidden, while Models Container is alpha 0 and is hidden.

When Seg 1 is selected, Profile Container is alpha 0 and is hidden, while Models Container is alpha 1 and not hidden.

When Seg 1 is selected, Profile Container is alpha 0 and is hidden, while Models Container is alpha 1 and not hidden.

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

            })

        }

    }
}

<小时>

要访问嵌入式视图控制器,请覆盖 prepareForSegue:

To access the Embedded View Controllers, override 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 存储库.

I've also updated the GitHub repo with an example of this.

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

I put this up as a sample project, if you'd like to dig into it: https://github.com/DonMag/SegmentsAndContainers

这篇关于使用 SegmentController 使 TableView 消失和 UIContainerView 出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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