2

我编写了一个具有 UIViewController 的应用程序,它在纵向模式下显示另一个 UIViewController,在横向模式下显示另一个 UIViewController。

当我去风景时,我会画/放置东西,所以我需要得到风景坐标。下面是当 iPhone 旋转时触发新视图的代码。下面的代码是这个视图的加载方式。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x
  duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(x)) 
{
    NSLog(@"Going Portrait");

} else if (UIInterfaceOrientationIsLandscape(x)) 
{
    NSLog(@"Going Landscape");
    if (activeView != [graphViewController view]) 
    {
        [containerView addSubview:[graphViewController view]];  
    }
}
}

我的问题是,当

-(void)loadView {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
NSLog(@"GraphViewController: Screenbounds %@", NSStringFromCGRect(screenBounds));
}

在 GraphViewController 返回时,它会产生: GraphViewController: Screenbounds {{0, 0}, {320, 480}}

这不是风景坐标,因此我的画是不正确的。如何使 GraphViewController 在调用[UIScreen mainScreen] bounds] 时具有正确的坐标;?

非常感谢

麦克风

4

1 回答 1

1

我认为您的问题在于您建立的层次结构。听起来像是当您添加第二个视图控制器时,它认为它处于纵向模式(即使您处于横向模式)。我认为解决您的问题的方法是实施

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation

主视图控制器和图形视图控制器中的方法。

但是,我认为这不是一条好的下坡路。您使用 uiviewcontrollers 而不仅仅是 uiviews 来显示不同的界面是否有特定的原因?

如果您可以直接使用 UIViews,然后执行类似的操作

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x
  duration:(NSTimeInterval)duration
{
   if (UIInterfaceOrientationIsPortrait(x)) 
   {
       NSLog(@"Going Portrait");

   } 
   else if (UIInterfaceOrientationIsLandscape(x)) 
   {
       NSLog(@"Going Landscape");
       if (activeView != graphView) 
       {
           containerView = graphView;
           //OR
           [containerView addSubview:graphView];
       }
   }
}

我认为从长远来看你会过得更好,但你的选择。

于 2011-02-15T02:11:10.720 回答