如何以编程方式创建具有横向方向的UIView?

WAS*_*D42 2 iphone cocoa-touch transform orientation uiview

我有一个固定横向的iPad应用程序.一切正常,直到我以编程方式创建UIView并将其添加到我的根UIViewController的视图时 - 它的方向始终设置为纵向,而所有其他内容都以横向模式显示.这个UIView是一个模态视图,显示了一些用户的自定义表单.

AlertView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
AlertView.opaque = NO;
AlertView.backgroundColor = [UIColor blackColor];
AlertView.alpha = 0.7;

<...> // some other configuration code

[window addSubview:AlertView];
Run Code Online (Sandbox Code Playgroud)

所以问题是:如何以横向方向启动UIView?我能想到的只是使用变换:

modalView.transform = CGAffineTransformMakeRotation( ( 180 * M_PI ) / 360 );
Run Code Online (Sandbox Code Playgroud)

虽然它不是太优雅的方式......

cdu*_*uhn 5

您的AlertView以纵向显示而其他应用程序以横向显示的原因是您将AlertView添加为窗口的子视图,而不是将其添加为视图控制器视图的子视图.这将它置于视图层次结构之外,通过自动旋转自动转换.

应用程序的窗口对象通过查找具有相应视图控制器的最顶层子视图来协调自动旋转.在设备旋转时,窗口调用shouldAutorotateToInterfaceOrientation:此视图控制器并根据需要转换其视图.如果您希望视图自动旋转,则它必须是此视图层次结构的一部分.

所以不要[window addSubview:AlertView];做类似的事情[self.view addSubview:AlertView];.