8

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?

2
  • but I am starting from 50 in y axis. Commented Dec 21, 2011 at 10:36
  • why you are assigning paintView to self.view self.view =paintView; Commented Dec 21, 2011 at 10:41

4 Answers 4

27

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];
  };
Sign up to request clarification or add additional context in comments.

4 Comments

Your custom implementation of this method should not call super. If you want to perform any additional initialization of your views, do so in the viewDidLoad method.
@Alexey, Maybe in your case, you don't need to implement the custom loadView. And this answer is some kind old. At iOS SDK 4/5, this answer is right. The [super loadView] will generate a empty view to self.view. And under iOS 7 now, I don't know that works or not.
There is a typo in the code and SO won't let me edit the answer. The method is "addSubview" not "addSubView".
Thank you very much. I had modify the answer.
3
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];

Comments

0

replace self.view =paintView; by

[self.view addSubview: paintView];

2 Comments

u have missed [super loadView] in ur loadview, add this then ur app won't crashes.
You should change your answer to include the correct solution from the comments.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.