2017-02-15 46 views
1

是否可以通過編程方式爲UIView設置viewController?我想要做的是製作一個總是覆蓋屏幕高度的50%的UIView,並在初始化後在該view上添加一個ViewController。我似乎無法找到一種方法。僞代碼:以編程方式在UIView上設置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(.......) 
+0

view.setViewController(.......)在這個地方使用view.addsubview(yourVCname.view) –

+0

@OleAndré所以你想有一個視圖控制器,其中50%被UIView覆蓋,處理由不同的視圖控制器? – Florensvb

+0

相關http://stackoverflow.com/a/42110439/6433023在視圖中添加ViewController –

回答

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 
0

我會隱藏和顯示視圖,而不是切換出整個UIViews所以需要#關閉

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) 
0
沒有更多的答案

您可以添加一個Container View,它使用它自己的UIViewController。只需將它從對象庫中拖出來,你就可以走了。

相關問題