在其他Controller的视图中添加pagecontroller视图时出错

时间:2019-09-19 10:08:37

标签: ios swift

我尝试通过实例图板实例化将pageViewController本身添加到控制器中的视图中,但是会产生以下错误代码

  • ViewController

    func instantiate(){
    
        let pageController = storyboard?.instantiateViewController(withIdentifier: String(describing:PageViewController.self)) as! PageViewController
    
        addChild(pageController)
        pageController.didMove(toParent: self)
        pageController.view.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(pageController) // error Cannot convert value of type 'PageViewController' to expected argument type 'UIView'
    
        let views:[String:Any] = ["pageView":pageController.view]
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[pageView]-0-|", options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: views))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[pageView]-0-|", options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: views))
    }
    

1 个答案:

答案 0 :(得分:3)

PageController继承自UIViewController。您正在做的就是添加与视图相同的视图。这就是为什么出现错误的原因,因为该参数不是UIView。

而不是添加pageviewcontroller,而是添加嵌入其中的视图。将行更改为contentView.addSubview(pageController.view)