5/5 Programmatically Create a Custom UIButton in Code with Objective-C
You can create a UIButton in code following these simple steps. Make sure you create a UIButtonTypeCustom button. Set the position and the size and then add it to the containing view.
Part 1/5: • 1/5 Custom Buttons in Xcode Interface Buil...
Part 2/5: • 2/5 Resizable Images for Custom iPhone But...
Part 3/5: • 3/5 Resizable Buttons with Custom Images f...
Part 4/5: • 4/5 Custom UIButton with Highlighted Backg...
Part 5/5: • 5/5 Programmatically Create a Custom UIBut...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Press Me" forState:UIControlStateNormal];
button.center = CGPointMake(320/2, 380);
// Resize to make it non-zero size
// [button sizeToFit];
button.bounds = CGRectMake(0, 0, 140, 44);
// Change font
button.titleLabel.font = [UIFont systemFontOfSize:15];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
// Add visual feedback
[button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
// Add Background Images
UIImage *blueButtonNormalImage = [UIImage imageNamed:@"BlueButtonNormal.png"];
blueButtonNormalImage = [blueButtonNormalImage resizableImageWithCapInsets:
UIEdgeInsetsMake(22, 22, 22, 22)];
UIImage *blueButtonHighlightedImage = [UIImage imageNamed:@"BlueButtonHighlighted.png"];
blueButtonHighlightedImage = [blueButtonNormalImage resizableImageWithCapInsets:
UIEdgeInsetsMake(22, 22, 22, 22)];
[button setBackgroundImage:blueButtonNormalImage forState:UIControlStateNormal];
[button setBackgroundImage:blueButtonHighlightedImage forState:UIControlStateHighlighted];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Click here to Subscribe to learn how to make iPhone apps. http://www.youtube.com/subscription_c...
Links:
Blog: http://iPhoneDev.tv
iPhone Courses: http://learn.iphonedev.tv/course/iPho...
Contact:
YouTube Channel: / paulsolt
Twitter: / paulsolt
Facebook: / paulsolt
Google+: https://plus.google.com/u/0/+PaulSoltInc
Paul's iPhone Apps: http://AppStore.com/PaulSolt