HideCode HideCode - 11 months ago 252
Swift Question

value of type 'UIViewController' has no member 'addSubview' Swift2

I have this error:

value of type 'UIViewController' has no member 'addSubview'

in this code. Do you know why?

override func viewDidLoad() {
self.currentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ComponentA")
self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.currentViewController?.view, toView: self.containerView)


self is an instance of UIViewController. It defines methods like viewDidLoad(), addChildViewController(), etc.

The method addSubview() is instead defined on the class UIView. If the view controller has a view set for its view property, you can access it with the call self.view.addSubview().