【问题标题】:Use SegmentController to Make TableView Disappear and UIContainerView Appear使用 SegmentController 让 TableView 消失,UIContainerView 出现
【发布时间】:2017-11-10 04:22:53
【问题描述】:

我正在尝试使用段控制器在我的 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(有效)切换到容器视图时的样子:

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

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

【问题讨论】:

  • 你能截取storyboard中的视图层次吗?
  • 已上传。 @LoryHuz
  • 哪个视图有绿色背景?您的帖子中不清楚
  • 而且...您以这种方式使用容器视图是否有原因?通常,您将在视图顶部放置分段控件,然后在其下方放置一个 ContainerView……然后您将在该容器视图交换表视图和模型视图。
  • 没有真正的理由使用堆栈视图...将您的段背景视图限制在顶部,将容器视图限制在段背景的底部和视图的底部。将“包含的视图”约束到容器视图的 4 个侧面。

标签: ios swift xcode uisegmentedcontrol


【解决方案1】:

试试这个方法...

Seg 背景视图高度为 45 点,顶部固定,前导,尾随都等于 0

Profile Container 的前导、尾随、底部都固定为0,顶部固定在 Seg Background 的底部。

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

Models Container 的宽度和高度相等,水平和垂直居中,全部为 Profile Container。

Profile Container 中嵌入了 Profile Table VC。

Models Container 中嵌入了 Models VC。

想法是:

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

选择Seg 1 时,Profile Container 为 alpha 0 且隐藏,而 Models Container 为 alpha 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

            })

        }

    }
}

编辑:

要访问嵌入式视图控制器,请覆盖 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

【讨论】:

  • 嗨@DonMag 我正在尝试从 ViewController 访问一个属性,该属性包含来自支持其中一个容器视图的 ViewController 中的容器视图,但我正在努力这样做。你有什么建议吗?似乎容器视图在包含容器视图的 ViewController 加载之前加载,因此我可以访问该属性,但该属性尚未设置。
  • hmmm...您有“MainVC”,其中包含您的开关和两个容器视图...您希望 MainVC 中的代码在容器中嵌入的一个(或两个)VC 中设置属性观看次数?
  • 容器视图中嵌入的一个 VC 中的属性
  • 但 MainVC 在容器视图中的 VC 之后加载,因此无法使用该属性
  • 在 MainVC 中,为要“通信”的 VC 创建一个变量(我暂时假设它是 ModelsVC)。当容器视图加载其嵌入的 VC 时,它会触发 prepare for segue。在 MainVC 中,override func prepare(for segue: UIStoryboardSegue, sender: Any?) .. 您可以在其中检查它是否是 ModelsVC... 如果它 ,则将其分配给您的变量。之后,您可以引用它及其属性/函数/方法/等。
猜你喜欢
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2021-08-19
  • 1970-01-01
  • 2017-11-22
相关资源
最近更新 更多