1

I am loading a view as soon as the user rotates to landscape but when I use view.bounds in viewDidLoad it is still in portrait.

Here the code where I load the view which is triggered from a notification

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        [self presentModalViewController:self.graphViewController animated:YES];
        isShowingLandscapeView = YES;
    } else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }
}

And in the graphViewController in viewDidLoad I use self.view.bounds to initialise a core-plot.

This is how it looks like

enter image description here

Any hint on how to fix that?

Thanks a lot

EDIT

in viewDidLoad

// Create graph from theme
    graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
    [graph applyTheme:theme];

    CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
    hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling
    hostingView.hostedGraph = graph;
    [self.view addSubview:hostingView];
Chris
  • 3,581
  • 8
  • 30
  • 51
  • How is the graph view being added on-screen? Are you trying to add it directly to the window? – CIFilter Sep 21 '11 at 16:27
  • [self presentModalViewController:self.graphViewController animated:YES]; – Chris Sep 21 '11 at 16:29
  • Yes, but within that view controller, how are you creating and adding the graph view. Please show more code. It's clear that the view controller is rotating correctly, so its subviews should as well. – CIFilter Sep 21 '11 at 16:31
  • I am not sure if I understood you right. I have added some code from viewDidLoad where I create a GraphHostingView and add it to self.view's subview. – Chris Sep 21 '11 at 16:36

2 Answers2

3

Assuming CPTGraphHostingView will adjust the orientation of how it renders, you need to set the autoresizingMask on it. This determines how views resize when their superviews resize, which is happening in this case.

hostingView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
CIFilter
  • 8,647
  • 4
  • 46
  • 66
0

It's not the correct approach. You should be doing this stuff in layoutSubviews. It will be called first time in, and subsequently each time orientation changes. Do something like this:

- (void)layoutSubviews 
{
    [super layoutSubviews];
    CGRect frame = self.bounds;

    // Do your subviews layout here based upon frame
}
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
  • Given that `CPTGraphHostingView` is part of Core Plot, a static library, I'm not sure this is an ideal solution because it requires the modification of the library. Instead, assuming autoresizing masks don't work as intended, the view controller's rotation methods would be more appropriate to adjust the frame of the graphing view. – CIFilter Sep 21 '11 at 16:40