0

我正在使用以下代码创建一个视图并将其放在顶部:

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
CGRect viewRect = mainWindow.frame;
topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[mainWindow addSubview:topView];

它工作得很好,但我的问题是如果我在视图中写任何东西(比如使用标签)并且我的设备处于横向位置,文本处于垂直位置。我附上了一张图片,以使其更清楚。有什么办法可以解决吗?在此处输入图像描述

编辑:如果我使用 UIApplication.sharedApplication.keyWindow.rootViewController 而不是 mainWindow,我会得到这个:

在此处输入图像描述

4

2 回答 2

0

如果不将给定的视图添加到视图控制器,就无法修复它(UIWindow从来没有打算处理旋转,并且没有这样做的逻辑)。

您遇到的不稳定的视图旋转结果实际上是 UIView 的 default 的autoresizingMask结果

topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[topViewsetAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
于 2013-03-07T23:25:34.517 回答
0

看到这个答案:

查看所有内容:UIWindow 子视图 VS UIViewController 子视图

基本上,只有主窗口的第一个子视图获得旋转事件,所以你必须以其他方式来做。

于 2013-03-07T23:26:13.753 回答