【问题标题】:Loading a ViewController inside a Container View在容器视图中加载 ViewController
【发布时间】:2015-05-05 07:35:32
【问题描述】:

我在 VC 中有一个全屏的 containerView。如果我从 Storyboard 手动将子项添加到 containerView,则进行嵌入 segue 看起来很好:

但如果我通过代码嵌入 VC:

class BannerContainerVC: UIViewController {

    @IBOutlet weak var container: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = storyboard?.instantiateViewControllerWithIdentifier("test") as UIViewController
        self.container.addSubview(vc.view)
    }
}

我得到了超级奇怪的结果:

【问题讨论】:

    标签: ios swift storyboard autolayout


    【解决方案1】:

    斯威夫特 5.0 使用此函数添加和删除子 VC:

    private func add(asChildViewController viewController: UIViewController, childFrame:CGRect) {
            // Add Child View Controller
            addChild(viewController)
    
            // Add Child View as Subview
            view.addSubview(viewController.view)
    
            // Configure Child View
            viewController.view.frame = childFrame
            viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
            // Notify Child View Controller
            viewController.didMove(toParent: self)
    
        }
        private func remove(asChildViewController viewController: UIViewController) {
            // Notify Child View Controller
            viewController.willMove(toParent: nil)
    
            // Remove Child View From Superview
            viewController.view.removeFromSuperview()
    
            // Notify Child View Controller
            viewController.removeFromParent()
        }
    

    取自here

    【讨论】:

      【解决方案2】:

      Swift 4.2/5 更新。

      let listVc = ListViewController() 
      listVc.view.frame = CGRect(x: 0, y: 0, width: self.listView.frame.width, height: self.listView.frame.height)
      addChild(listVc)
      listVc.didMove(toParent: self)
      

      ListViewController 是另一个要嵌入的视图控制器。

      【讨论】:

        【解决方案3】:

        只要调用这个函数:-

        private func addChildView(viewController: UIViewController, in view: UIView) {
            viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
            viewController.view.frame = view.bounds
            addChild(viewController)
            view.addSubview(viewController.view)
            viewController.didMove(toParent: self)
         }
        

        【讨论】:

          【解决方案4】:

          为 Swift 4 更新

          self.addChildViewController(vc)
          vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height)
          self.container.addSubview(vc.view)
          vc.didMovestrong text(self)
          

          【讨论】:

            【解决方案5】:

            尝试使用上面的答案,但结果CGRectMake 不再可用。

            为 Swift 3 更新

            self.addChildViewController(vc)
            vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height)
            self.container.addSubview(vc.view)
            vc.didMoveToParentViewController(self)
            

            【讨论】:

            • 和 vc.didMoveToParentViewController(self) 现在是 vc.didMove(toParentViewController: self)
            • @LuizDias 这是否允许容器视图的动态高度?
            • 是的,@ethanfox27。但是您需要在self.container.frame.size.widthself.container.frame.size.height 处提供正确的帧大小
            【解决方案6】:

            您需要告诉 BannerContainer 视图控制器它有一个新的子控制器,并告诉 Child 它有一个父 VC。这在 Apple Docs here 中有描述。像这样:

               [self addChildViewController:vc];
               vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
               [self.container addSubview:vc.view];
               [vc didMoveToParentViewController:self];
            

            或者在 Swift 中:

                self.addChildViewController(vc)
                vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
                self.container.addSubview(vc.view)
                vc.didMoveToParentViewController(self)
            

            这样可以保证各种布局和触摸方法都传递给子VC;我怀疑您遇到的布局问题可能是由于当前未调用这些方法。

            【讨论】:

            • 确实解决了我在主视图上的约束问题
            • 如何快速从 containerView 中移除 viewController?因为一旦我添加了一个 viewController ,然后是另一个,第一个仍然是嵌入的?
            • 请参阅我的答案中嵌入的链接中的“删除子视图控制器”。本质上你只需要反转这个过程:在孩子上调用willMoveToParentViewController,删除视图,然后调用removeFromParentViewController。 @AymenBRomdhane
            • 我找到了这个解决方案,但我不知道如何使用它。 func hideContentController(content: UIViewController) { content.willMoveToParentViewController(nil) content.view.removeFromSuperview() content.removeFromParentViewController() }
            • 如果没有更多信息,很难提供帮助 - 最好放在一个新问题中,而不是 cmets。在这里加个链接,我去看看。
            猜你喜欢
            • 2016-11-29
            • 1970-01-01
            • 1970-01-01
            • 2015-12-11
            • 1970-01-01
            • 1970-01-01
            • 2013-07-25
            • 2015-01-26
            • 1970-01-01
            相关资源
            最近更新 更多