2011-09-21 34 views
1

我加載視圖,只要用戶旋轉到風景,但是當我在viewDidLoad中使用view.bounds它仍然是肖像。界限何時更新?

這裏,我加載它從通知

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

,並在viewDidLoad中我使用self.view.bounds初始化一個核心情節graphViewController觸發視圖的代碼。

這是怎麼看起來像

enter image description here

如何解決這個問題的任何提示?

非常感謝

編輯

在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]; 
+0

如何在屏幕上添加圖形視圖?你是否試圖直接將其添加到窗口? – LucasTizma

+0

[self presentModalViewController:self.graphViewController animated:YES]; – Chris

+0

是的,但在該視圖控制器中,您如何創建和添加圖形視圖。請顯示更多代碼。很明顯,視圖控制器正在旋轉,所以它的子視圖也應該如此。 – LucasTizma

回答

3

假設CPTGraphHostingView將調整其呈現的方位,你需要設置就可以了autoresizingMask。這決定了當他們的超級視圖重新調整時視圖的重新調整大小,這種情況正在發生。

hostingView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
+0

應該是autoresizingMask小錯字,但你是男人!這是完美的。非常感謝 – Chris

+0

哎呀!尷尬。 :)但我很高興你得到它! – LucasTizma

0

這不是正確的做法。你應該在layoutSubviews中做這件事。它將在第一次被調用,並且隨後每次改變方向。做這樣的事情:

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

    // Do your subviews layout here based upon frame 
} 
+0

鑑於'CPTGraphHostingView'是Core Plot(靜態庫)的一部分,我不確定這是一個理想的解決方案,因爲它需要修改庫。相反,假設自動調整掩碼無法按預期工作,視圖控制器的旋轉方法將更適合調整圖形視圖的框架。 – LucasTizma