NoviceDeveloper
NoviceDeveloper

Reputation: 225

creating uiview programmatically?

Creating a view with following code.

  - (void)loadView {

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

But my problem is whole screen fills with yellow color, but I want with specified frame. I am creating this view in a view based application.. Am i doing something wrong, Overridind the original view?

Upvotes: 8

Views: 45315

Answers (4)

AechoLiu
AechoLiu

Reputation: 18368

You can do it in following way.

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

Upvotes: 27

Ravi Kumar Karunanithi
Ravi Kumar Karunanithi

Reputation: 2218

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

Upvotes: 3

Aadil
Aadil

Reputation: 703

I am Going change Line No 3 in LoadView Method , you should add the subView in main View instead on assiging it Directly.

[self.view addSubview:paintview];

Upvotes: 0

iCoder4777
iCoder4777

Reputation: 1692

replace self.view =paintView; by

[self.view addSubview: paintView];

Upvotes: 0

Related Questions