以编程方式创建uiview?

时间:2011-12-21 10:29:37

标签: ios uiview

使用以下代码创建视图。

  - (void)loadView {

paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[paintView setBackgroundColor:[UIColor yellowColor]];
self.view = paintView;
[paintView release];

但我的问题是整个屏幕填充黄色,但我想要指定的框架。 我在基于视图的应用程序中创建此视图。我做错了什么,Overridind原始视图?

4 个答案:

答案 0 :(得分:24)

您可以通过以下方式完成。

  - (void)loadView {
    /// make a empty view to self.view 
    /// after calling [super loadView], self.view won't be nil anymore. 
    [super loadView]; 

    paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [paintView setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:paintView];
    [paintView release];
  };

答案 1 :(得分:2)

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = @"Mytext";
mytext.scrollEnabled=NO;
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];

答案 2 :(得分:0)

替换self.view =paintView;

[self.view addSubview: paintView];

答案 3 :(得分:0)

我要在LoadView方法中更改第3行,你应该在主视图中添加subView而不是直接分配它。

[self.view addSubview:paintview];