This is some code I used to put a reset button on the screen. It’s mostly self-documenting. First, create a rect. I’ve already defined the x and y coordinates—buttonX and distanceFromTop based on the screen width and other buttons on the screen. Likewise, I’ve already defined the width. The rest of the code is just assigning properties to the button. The action is a method in the file that changes the image to a ‘Selected’ image.
// Reset Button
@synthesize resetButton = resetButton;
....
CGRect resetButtonFrame = CGRectMake(buttonX, distanceFromTop,
button_width, 25.0f);
self.resetButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.resetButton.titleLabel.font = [UIFont systemFontOfSize: 16];
self.resetButton.titleLabel.textColor = [UIColor blueColor];
self.resetButton.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[self.resetButton setTitle:@"Reset Scoring" forState:UIControlStateNormal];
[self.resetButton setFrame:resetButtonFrame];
[self.resetButton addTarget:self
action:@selector(resetScorekeeper:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.resetButton];
// This line makes the icons stay in the center of the screen when you rotate
self.resetButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
....
- (IBAction)resetScorekeeper:(UIButton *)sender {
[self resetResultsFile];
[sender setImage:[UIImage imageNamed:@"ResetScoringSelected.png"] forState:UIControlStateNormal];
}