首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SegmentController使TableView消失,UIContainerView出现

使用SegmentController使TableView消失,UIContainerView出现
EN

Stack Overflow用户
提问于 2017-10-26 18:18:50
回答 1查看 152关注 0票数 0

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

这是我的代码:

代码语言:javascript
复制
@IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        profileTableView.isHidden = false
        modelsContainerView.isHidden = true
    } else {
        profileTableView.isHidden = true
        modelsContainerView.isHidden = false
    }
}

更新

如果我使用这个代码,模拟类的工作。出现容器视图,但它不像tableview那样填充屏幕。

代码语言:javascript
复制
    @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切换到容器视图时的样子:

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

非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-26 20:12:13

试试这个方法..。

Seg背景视图的高度为45秒,并固定在顶部,引导,尾随都等于0.

配置文件容器被固定为前导、尾随、底部都等于0,顶部固定在Seg背景的底部。

但是您看不到配置文件容器(红色背景),因为模型容器(橙色背景)就在上面,而且.

模型容器是相同的宽度和高度,中心水平和垂直,所有的配置文件容器。

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

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

这样做的目的是:

当选择Seg 0时,配置文件容器是alpha 1,而不是隐藏,而模型容器是alpha 0,隐藏的。

当选择Seg 1时,配置文件容器是alpha 0,隐藏的,而模型容器是alpha 1,不是隐藏的。

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46961231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档