边界什么时候更新?

时间:2011-09-21 16:24:26

标签: iphone viewdidload bounds

我在用户旋转到横向时立即加载视图,但是当我在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的graphViewController中,我使用self.view.bounds来初始化核心图。

这就是它的样子

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

2 个答案:

答案 0 :(得分:3)

假设CPTGraphHostingView将调整其呈现方向,您需要在其上设置autoresizingMask。这决定了在超级视图调整大小时视图的调整大小,在这种情况下会发生这种情况。

hostingView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

答案 1 :(得分:0)

这不是正确的做法。你应该在layoutSubviews中做这些事情。它将在第一次调用,随后每次定向更改。做这样的事情:

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

    // Do your subviews layout here based upon frame
}