2011-10-20 48 views
1

我在我的第一个iPad项目中使用了Core Plot 0.9。我需要创建一个水平条形图,并在其左侧创建与每个条形图的中心对齐的UIButtons,每个条形图一个按钮,请参阅附件screenshot将UIButtons对齐到水平核心图的条形图CPTBarPlot

我的设置是相当简单:

我有一个自定义UIView子类是作为一个容器。我有2个子视图:一个CPTGraphHostingView和一个UIView来包含我的UIButtons。 我在我的CPTGraphHostingView中托管一个CPTXYGraph,并分配一些填充值。

在我的自定义类,我有一次在数据源已定,将创建按钮的方法:

for (int i = 0; i < nbRecords; i++) { 

    // grab the bar location and create a plot point for it 
    NSNumber* loc = [dataSource numberForPlot:plot field:CPTBarPlotFieldBarLocation recordIndex:i]; 
    double plotPoint[2] = {0, [loc doubleValue]}; 

    // Convert the plot point to plot area space coordinates (I guess ;)) 
    CGPoint viewPoint = [graph.defaultPlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:plotPoint]; 

    // Convert the view point to the button container layer coordinate system   
    CGPoint p = [graph convertPoint:viewPoint fromLayer:graph.plotAreaFrame.plotArea]; 

    // Create the button 
    CGRect rect = CGRectMake(0, p.y - 11, 100, 22); 
    UIButton *btn = [[UIButton alloc] initWithFrame:rect]; 
    [btn setTitle:@"test" forState:UIControlStateNormal];   
    [buttonView addSubview:btn]; 
    [self addSubview:btn]; 
    [array addObject:btn]; 
    [btn release]; 
} 

按钮被创建,但定位并不好......我发现,如果我设置所有图形填充为0,然后按钮对齐(但当然我需要填充)。

我错过了某些东西或点的转换步骤?我有点困惑如何将plotAreaPoint坐标转换为任何其他UIView ...!

回答

0

好的,我自己想出了这个问题。事实上,我在图形有机会布局之前添加了按钮。在循环解决对齐问题之前调用图的方法-layoutIfNeeded: