我尝试通过实例图板实例化将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))
}
答案 0 :(得分:3)
PageController继承自UIViewController。您正在做的就是添加与视图相同的视图。这就是为什么出现错误的原因,因为该参数不是UIView。
而不是添加pageviewcontroller,而是添加嵌入其中的视图。将行更改为contentView.addSubview(pageController.view)