开发者

bounds] not changing when going landscape

I have written an application that has a UIViewController which displays another UIViewController when in portrait mode and a different UIView开发者_开发知识库Controller when in landscape.

When I go landscape I will be drawing/placing things so I need to get the landscape co-ordinates. Below is the code that triggers the new view when the iPhone rotates. The code below is how this view is loaded.

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

My problem is that when the

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

in GraphViewController returns, it produces: GraphViewController: Screenbounds {{0, 0}, {320, 480}}

which isn't the landscape co-oridinates and thus my drawing is incorrect. How do I make it so GraphViewController has the correct co-ordinates when it calls [UIScreen mainScreen] bounds];?

Many thanks

Mike


I think your problem is with the hierarchy you have built. What it sounds like is when you add in the 2nd view controller, it think's its in portrait mode (even though you are in landscape). I think what will fix your problem is implementing the

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation

methods in both your main viewcontroller and the graphviewcontroller.

However, I don't think this is a good path to go down. Is there a specific reason you are using uiviewcontrollers instead of just uiviews to display the different interfaces?

If you can go to straight UIViews, and then just do something like

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

I think you will be better off in the long run, but your choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜