添加子视图时,如果应用程序处于横向模式,则视图不会调整大小
我有一个在Interface Builder中创建并配置为根据方向调整大小的视图。如果在应用程序处于纵向模式时创建视图并将其添加到显示中,则一切都很完美。然后我可以旋转设备/模拟器,视图调整大小如我所料。但是,如果在应用处于横向模式时添加视图,则无法正确调整大小。视图的创建和大小就像应用程序仍处于纵向模式一样。该视图添加了以下代码:
I have a view that has been created in Interface Builder and configured to resize according to the orientation. If the view is created and added to the display while the application is in portrait mode everything works perfectly. I can then rotate the device/simulator and the view resizes as I would expect. But, if the view is added when the app is in landscape mode it does not resize correctly. The view is created and sized as if the app is still in portrait mode. The view is added with the following code:
self.viewController = [[MyViewController alloc] initWithNibName:nibName bundle:bundle];
[self addSubview:self.viewController.view];
这里没有什么神奇的事情发生。此外,视图控制器指定:
Nothing magical happening here. Additionally, the view controller specifies:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
同样,只要在纵向中添加视图,它就会按预期工作模式。我尝试了几种不同的东西,包括在视图上手动设置autoresizingMask属性,调用setNeedsLayout,以及调用layoutIfNeeded,但似乎没有任何工作。我能够手动设置框架的宽度和高度以强制视图调整大小,但这会破坏视图的动态特性。例如,如果我手动设置宽度,则不会调整高度,因为内容不会重新布局。
Again, it works as expected as long as the view is added while in portrait mode. I've tried several different things including manually setting the autoresizingMask property on the view, calling setNeedsLayout, and calling layoutIfNeeded, but nothing seems to be working. I am able to manually set the width and height of the frame to force the view to resize, but this breaks the dynamic nature of the view. For example, if I manually set the width the height doesn't adjust as the content doesn't re-layout.
任何想法?
只需设置你的子视图框架
Just set your subview frame like this
self.viewController.view.frame = self.view.bounds;