iPhone Create SubView
In this tutorial, we are going to show you how to create & add subView onto the UIView.
You can either create and add the subView in interface builder or can draw it in load View method. The example will illustrate both the ways.
Create and add a SubView in UIView programatically - Create a new View based application and in the implementation file(xyzViewController.m), uncomment the view did load method and paste the given code inside it.
CGRect myFrame = CGRectMake(0, 0, 320, 50);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor blueColor];
Just look at the code, in the code first we are creating a frame and then view. We have set the color to subview and added it onto the UIView through "[self.view addSubview:myView];".
Foregoing code is a programatic approach of creating a subView.
Now lets find out how you can do it in interface builder.
First of all in your application ViewController.h file, create a "IBOutlet UIView* myheader;" and write a property for it. Now you need to synthesize and release it into the ViewController.m file. In the same ViewController.m file create a interface as i did ...
- (void) configureTableHeader;
just above the implementation.
Now, write the method as given in implementation and call it into the ViewDidLoad method.
- (void) configureTableHeader
Open the ViewController.xib file in interface builder and add a subview, name it as header and connect it to the IBOutlet method in file owner. Save and build the application.
It should look like as given image...
You can also add labels, text, background color or images onto the sub view in interface builder as i did in my application.