以编程方式在UIView上设置ViewController

时间:2017-02-15 12:11:53

标签: ios swift view autolayout viewcontroller

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

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(.......)

4 个答案:

答案 0 :(得分:0)

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 

答案 1 :(得分:0)

我会隐藏并显示视图而不是切换整个UIViews所以不再需要答案#close

答案 2 :(得分:0)

// 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 :(得分:0)

您可以添加使用自己的UIViewController的Container View。只需将其从对象库中拖出即可,您可以轻松上手。