我有一个简单的 UIViewController,它使用 XIB 作为其界面。

因此,界面简单地包括UIView、UIActivityIndi​​cator和UILabel。我在 Interface Builder 中设置了大小限制,以在视图旋转时保持活动指示器和标签居中。

UIViewController 设置为在纵向和横向方向上返回 YES -shouldAutorotateToInterfaceOrientation: 方法。

视图被手动添加到视图层次结构中,使用

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);
 }

}

这里我根据当前设备方向更改位置。也许如果您检测到当前方向是横向,您可以添加具有此类方向的子视图。

亚历杭德拉:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top