手动添加到层次结构的视图旋转到横向(iPhone)

手动添加到层次结构的视图旋转到横向(iPhone),iphone,uiviewcontroller,rotation,orientation,Iphone,Uiviewcontroller,Rotation,Orientation,我有一个简单的UIViewController,它使用XIB作为接口 因此,该接口仅包括UIView、UIActivityIndicator和UILabel。我在Interface Builder中设置了大小限制,以便在视图旋转时保持活动指示器和标签居中 在-shouldAutorotateToInterfaceOrientation:方法中,UIViewController设置为对纵向和横向方向返回YES 使用手动将视图添加到视图层次中 if (!activityOverlay) ac

我有一个简单的UIViewController,它使用XIB作为接口

因此,该接口仅包括UIView、UIActivityIndicator和UILabel。我在Interface Builder中设置了大小限制,以便在视图旋转时保持活动指示器和标签居中

-shouldAutorotateToInterfaceOrientation:
方法中,UIViewController设置为对纵向和横向方向返回YES

使用手动将视图添加到视图层次中

if (!activityOverlay)
    activityOverlay = [[XBActivityOverlayViewController alloc] initWithNibName:@"XBActivityOverlayViewController" bundle:nil message:@"Connecting..."];

[activityOverlay.view setAlpha:0.0f];
[self.window addSubview:activityOverlay.view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[activityOverlay.view setAlpha:0.9f];
[UIView commitAnimations];
我遇到的问题是,如果设备已经是横向的,并且此时视图被添加到层次结构中,那么视图仍然是纵向的,并且不会自动旋转


我需要做些什么才能将视图添加到与父视图相同的方向?

我不知道这是否是您问题的正确答案,但我希望能提供帮助:

对于我来说,每次添加/取消模式或显示新视图时,都会调用以下函数:

-(void) detectOrientation 
{


 if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
 {

             item1.frame = CGRectMake(147.0, 241.0, 56.0, 28.0);
     item2.frame = CGRectMake(265.0, 241.0, 56.0, 28.0);

 }
 else if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) ||
          ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) || !inLandscapeOrientation)
 {

    item1.frame = CGRectMake(35.0, 425.0, 35.0, 28.0);
item2.frame = CGRectMake(176.0, 425.0, 35.0, 28.0);
 }

}
在这里,我根据当前设备方向更改位置。如果检测到当前方向为横向,则可能会添加具有该方向的子视图

亚历杭德拉:)