ios - 有没有办法将我的 `scrollView` 从我的主类连接或访问到另一个类?

标签 ios swift scrollview

在我的 MainViewController 中,我有一个 scrollView 并在其中添加一个 ChildView,效果很好。在此示例中,我实例化了我的 PlansViewController2 并将其作为 ChildView 添加到我的 scrollView

enter image description here

选择一个 Cell 后,它会推送一个新的 Controller 。但我想将新 Controller 作为 ChildView 添加到我的 `scrollView。

enter image description here


  @IBAction func planAction(_ sender: UIButton) {
    let newPlan = sender.frame.origin.x
    scrollView.subviews.forEach{$0.removeFromSuperview()}
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let nextController = storyBoard.instantiateViewController(withIdentifier: "PlansViewController2") as! PlansViewController2
    addChildView(viewController: nextController, in: scrollView)
    self.movingView.frame.origin.x = newPlan
}

我是我的 PlansViewController2 我推了另一个 ViewController 但我还想将它添加到我的 scrollView 来自上一个 Controller 。

let storyBoard = UIStoryboard(name:"Main", bundle:nil)
            let planViewCtrl = storyBoard.instantiateViewController(withIdentifier: "PlanViewCtrl") as! PlanViewController
            planViewCtrl.navigationItem.title = plan.title
            self.show(planViewCtrl, sender: self)
            _ = planViewCtrl.view // force view loading, so we can set it up properly
            _ = self.updateLastUsed(plan: plan)
            planViewCtrl.plan = plan

是否可以将 PlanViewController 从我的 MainViewController 添加到我的 scrollView 中?

最佳答案

当您将 View 的 PlansViewController2 添加到 ScrollView 时。请将 PlansViewController2 添加到 VC 的 childViewController 中,有 scrollView(self.addChild(nextController))。 当你想将 PlanViewController 添加到 scrollView 时:

  • 第 1 步:首先,您需要使用 parent 属性 (self.parent) 获取具有 scrollView 的 viewcontroller。
  • 第 2 步:最后,您可以通过在第 1 步中获得的 vc 访问 scrollView 实例。

祝你好运!

关于ios - 有没有办法将我的 `scrollView` 从我的主类连接或访问到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56883193/

相关文章:

ios - 它总是在 iOS swift 中返回相同的索引 "picker.selectedRow(inComponent: 0)"

ios - 如何让 UIAnimation 一次执行一个(迭代 for 循环),而不是一次执行全部?

swift - 滚动条错误地显示在 UICollectionView 部分标题下方

ios - 在 Swift 中重新加载 subview

ios - 序列化 JSON 而不用 [ ] 括起来

ios - UISwitch 未根据值正确设置

Android Scrollview 布局问题

android - 如何使 ScrollView 填充其父级的剩余空间?

java - ScrollView 剪切 TextView 中的文字

ios - 将数组从 objc 传递到 Swift