简体   繁体   中英

Autolayout - view controller frame incorrect

I'm trying to figure out why the frame of my view controller (loaded from a xib) is set properly when presented via presentViewController:

self.presentViewController(testVC, animated: true, completion: nil);

...but remains at (0, 0, 600, 600) when added as a child view controller:

self.addChildViewController(testVC)
self.view.addSubview(testVC.view)
testVC.didMoveToParentViewController(self)
testVC.view.setNeedsLayout(); (makes no difference)

I have an isolated example that I can provide that demonstrates the issue more clearly. Ideas?

Try This.

self.addChildViewController(testVC)
testVC.view.frame=self.view.bounds//or set by CGRectMake()
testVC.willMoveToParentViewController(self)
self.view.addSubview(testVC.view)
testVC.didMoveToParentViewController(self)

It's because you forgot to give it a frame! You are saying:

self.view.addSubview(testVC.view)

But you neither apply a frame nor attach any constraints, so the view is not resized. You are now the parent. Resizing the child view is up to you . If you don't, its size is the size it had in the storyboard — 600 by 600, just as you said. It doesn't get resized by magic. You have to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM