Personally I'm not a fan of Interface Builder for designing layout of iPhone applications. I prefer the flexibility of having visual elements generated by code where possible. Here are some examples of how to work without IB.
in the view controller you get when you start a view based project add the code below:
- (void)loadView {
MyView *view = [[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = view;
}
You will need to make sure you import the relevant file, in this case: #import "MyView.h"
Then create a file called MyView which extends UIView. this is then the view instead of the relevant .xib file, which can be deleted if it exists. You can then add elements to the view within the view file or within the view controller and avoid interface builder altogether.
//below shows how to add elements to the view
Here I will add an image to the screen named "myImage.png" at the position x:20 y:20 with a width of 30 and height of 93.
UIImage* img = [UIImage imageNamed:@"myImage.png"];
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20,20, 30, 93)];
imgView.image = img;
[self.view addSubview:imgView];
note: self.view is used if the code is placed in the related viewDidLoad method of the ViewController.m file. If it is in the View you would just use [self addSubview:imgView].
In order to use images as buttons you need to make sure they are userInteractionEnabled, this is set to NO for UIImageViews by default.
UIImage* img = [UIImage imageNamed:@"myImage.png"];
UIImage* img_down = [UIImage imageNamed:@"myImage_downstate.png"];
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20,20, 30, 93)];
imgView.image = img;
imgView.userInteractionEnabled = YES;
[self.view addSubview:imgView];
Then for press states in your touchesBegan set imgView.image = img_down; be sure to set it back on your touchesEnded method. Here is how these methods might look:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if([touch view]==imgView){
imgView.image = img_down;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
imgView.image = img;
if([touch view]==imgView){
//execute whatever you want
}
}