2010-09-23 51 views
3

在我的项目中,我有一个自定义的@interface GraphView: UIView。因此GraphViewUIView的一个子类,意在显示一个图。如何使用[self.view addSubview:myView]添加子视图,其中myView是UIView的子类?

然后,我使用NIB创建了一个名为Summary的新视图控制器。在“界面”构建器中,我只在视图的底部添加一个UIToolbar

Summary实施,在viewDidLoad方法,我有以下代码:

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    GraphView *myGraph = [[GraphView alloc]init]; 
    [self.view addSubview:myGraph]; //this does not work 
} 

但我无法得到这一点。

回答

3

我相信你需要在将它作为子视图添加到self之前设置myGraph的帧。

CGRect rect = CGRectInset(self.view.bounds, 0.0, 0.0); 
GraphView *myGraph = [[GraphView alloc] initWithFrame:rect]; 
[self.view addSubview:myGraph]; 
相关问题