添加子视图时,如果应用程序处于横向模式,则视图不会调整大小 [英] When adding a sub view the view does not resize if the app is in landscape mode

查看:143
本文介绍了添加子视图时,如果应用程序处于横向模式,则视图不会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在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;

这篇关于添加子视图时,如果应用程序处于横向模式,则视图不会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆