Joey Yi Zhao
Joey Yi Zhao

Reputation: 42490

How to manage nested viewcontrooler view size in swift?

I have a parent view controller and want to build a nested view controller inside it. The parent view controller has a contentView(a UIView) in the middle of the screen. When a button clicked, I want the nested view controller is presenting in the contentView position. But when I click the button the nested view will show outside of the contentView. Below is the code I am using. The currentViewController is the nested view. And the contentView is the parent view. The interesting thing I found is that if I am using UITableViewController as the nested controller, everything works fine. The nested view is located correctly inside its parent view. It failed when I am using UIViewController. I could not figure out what's going wrong.

self.currentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("NestedViewController")
self.addChildViewController(self.currentViewController!)
        self.currentViewController?.view.translatesAutoresizingMaskIntoConstraints = false
        self.currentViewController?.view.frame = self.contentView.bounds
        self.contentView.addSubview((self.currentViewController?.view)!)
        self.currentViewController?.didMoveToParentViewController(self)
        self.currentViewController?.view.layoutIfNeeded()

Upvotes: 0

Views: 79

Answers (1)

Yunus Eren Güzel
Yunus Eren Güzel

Reputation: 3088

You should remove the line

self.currentViewController?.view.translatesAutoresizingMaskIntoConstraints = false

since it is used when you want to use auto layout constraints.

Upvotes: 1

Related Questions