self.view = someController.view versus [self.view addSubview:someController.view]
I'm trying to figure out a bug I have. Originally, my coworker just added views to the subview using the
[self.view addSubview:someController.view];
from the current ViewController. This looks right, but is kind of sluggish. I wanted to lazily load the views as needed and use the advice I got in this post: How to optimize performance in view controller navigation with UISegmentedControl and UITabBarController
It does work faster by just setting the view like
self.view = someControll开发者_如何学编程er.view
however, the view's height is incorrect. I'm trying to debug what is happening, but I'm not sure if I understand the difference between adding the new view as a subview, vs setting it to my current view. I would think that adding it on my current view, or adding it as a subview would take up the same size, but I must be missing something. Any help is appreciated. Thanks!
self.view = someController.view, means that the view is replaced by the new view. This means that the new view will have all the properties setted in someController.view.
Adding a view as asubview means that the new view will have to face with everything has been set in the parent view (starting from frame, size and margins).
Each of this methods have different meanings and should be used for different results.
I don't know which is your, but if you just want to have the replacing view of the same size of the replaced one, you can just use someController.view.frame = self.view.frame;
before adding or replacing the old view.
AFAIK when view is added as a subview, the parent view looks at its autosizing mask and adjusts the view size accordingly. Not unlike "replacing" the view itself with another view where size adjustment does not take place at all.
The first one completely replaces self.view
with someController.view
.
The second one has someController.view
added to your self.view
. This means that self.view
will be visible except for the frameSize
of someController.view
.
Please check Documentation .
精彩评论