This is a discussion on How To Create UIButton Programmatically within the iPhone/iPad Programming Tutorials forums, part of the Programming Section category; Hey guys, Here I'll show you how to create UIButton programmatically. Code: UIButton *htmlCopy = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; htmlCopy.frame = ...
Hey guys,
Here I'll show you how to create UIButton programmatically.
Code:UIButton *htmlCopy = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; htmlCopy.frame = CGRectMake(246, 207, 71, 31); [htmlCopy setTitle:copybuttonstring forState:UIControlStateNormal]; [htmlCopy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; htmlCopy.backgroundColor = [UIColor clearColor] [htmlCopy addTarget:self action:@selector(EmailCopyEvent:) forControlEvents:UIControlEventTouchUpInside]; [htmlCopy setBackgroundImage:[UIImage imageNamed:@"chrome.png"] forState:UIControlStateNormal];
Code explanation:
1)This will create the button with type "Custom", you can chose deferent types like rounded etc..Code:UIButton *htmlCopy = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
2)This will define the location and size of button (x axes, y axes, width , height).Code:htmlCopy.frame = CGRectMake(246, 207, 71, 31);
3)Sets the title of the button.Code:[htmlCopy setTitle:@"Set Title" forState:UIControlStateNormal];
4)Sets the color of the title.Code:[htmlCopy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
5)Sets background color.Code:htmlCopy.backgroundColor = [UIColor clearColor];
6)Sets an action for button.Code:[htmlCopy addTarget:self action:@selector(EmailCopyEvent:) forControlEvents:UIControlEventTouchUpInside];
7)Sets background image of button.Code:[htmlCopy setBackgroundImage:[UIImage imageNamed:@"chrome.png"] forState:UIControlStateNormal];
MozyMac Founder,Chairman and CEO
MacBook Pro Unibody late 2008 2.4Ghz 4GB ram 250GB HD
MozyMac Youtube Channel