代码之家  ›  专栏  ›  技术社区  ›  Jasarien

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

  •  0
  • Jasarien  · 技术社区  · 16 年前

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

    因此,界面只包含一个uiview、一个uiactivityindicator和一个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];
    

    我遇到的问题是,如果设备已经处于横向,并且此时视图被添加到层次结构中,那么视图仍然处于纵向,并且不会自动旋转。

    要添加与父视图方向相同的视图,需要做什么?

    1 回复  |  直到 16 年前
        1
  •  1
  •   Alejandra Gonzalez    16 年前

    我不知道这是否是你问题的正确答案,但我希望能有所帮助:

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

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

    在这里,我根据当前设备方向更改位置。如果你发现当前方向是横向的,你可以添加一个子视图。

    阿莱杭德娜:)