【问题标题】:Set the ViewController on UIView programmatically以编程方式在 UIView 上设置 ViewController
【发布时间】:2017-07-04 02:18:26
【问题描述】:

是否可以通过编程方式为UIView 设置viewController?我想要做的是制作一个始终覆盖 50% 屏幕高度的UIView,并在初始化后在该view 上添加一个ViewController。我似乎找不到方法。伪代码:

let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height UIScreen.main.bounds.height * 0.5)

//Can't seem to find a method that does exactly this:
view.setViewController(.......)

【问题讨论】:

  • view.setViewController(.......) 在这个地方使用 view.addsubview(yourVCname.view)
  • @OleAndré 所以你想要一个视图控制器,其中 50% 被 UIView 覆盖,由不同的视图控制器处理?
  • Related stackoverflow.com/a/42110439/6433023 用于在视图中添加 ViewController
  • @FlorensvonBuchwaldt 是的,我有一个主视图控制器,里面有 UIViews,我想要不同的视图控制器来布置这些视图

标签: ios swift view autolayout viewcontroller


【解决方案1】:

您可以添加一个使用自己的 UIViewController 的容器视图。只需将其从对象库中拖出即可。

【讨论】:

    【解决方案2】:
    // Add Child View Controller from your MainVC
        addChildViewController(viewController) //your 2nd VC
    
        // Add Child View as Subview
        view.addSubview(viewController.view)
    
        // Configure Child View
        viewController.view.frame = view.bounds //here you make it 50% of height!
        viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
        // Notify Child View Controller
        viewController.didMove(toParentViewController: self)
    

    【讨论】:

      【解决方案3】:

      我将隐藏和显示视图,而不是切换整个 UIView,因此不需要更多答案#close

      【讨论】:

        【解决方案4】:

        yourAnotherVC.view 作为子视图插入yourview

        var MenuView: AnotherViewController? = (self.storyboard?.instantiateViewController(withIdentifier: "NewsView") as? AnotherViewController)
            self.addChildViewController(MenuView)
            MenuView?.didMove(toParentViewController: self)
            MenuView?.view?.frame = CGRect(x: CGFloat(0.0), y: CGFloat(self.containerView.frame.size.height), width: CGFloat(self.containerView.frame.size.width), height: CGFloat(self.containerView.frame.size.height))
            yourview.insertSubview(MenuView?.view, belowSubview: self.bottomMenuView)
        
            //Show presentation logic here forward i mean show Anotherviewvc bottom to top or top to bottom 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-18
          • 1970-01-01
          • 2020-07-03
          • 2011-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-23
          相关资源
          最近更新 更多