iOS Create UIButton Programmatically in Objective-C
Are you beginner in iOS Development ?
Or you facing any problem with UIButton?
Today, I am writing post about how to create UIButton programmatically.
Where you can learn,
how to set UIButton image,
how to set UIButton text,
how to set UIButton background image,
how to change UIButton font
how to add click event on UIButton
So what are you waiting for now ?
Just open Xcode, Create new project & create UIButton programmatically.
Download iOS Code here : Click to Download
So what are you waiting for now ?
Just open Xcode, Create new project & create UIButton programmatically.
Download iOS Code here : Click to Download
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create Button object
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set Button title
[button setTitle:@"Click Me" forState:UIControlStateNormal];
// Set Button frame
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
//Set button background image
[button setBackgroundImage:[UIImage imageNamed:@"bg.png"] forState:UIControlStateNormal];
// Set button Image
// [button setImage:[UIImage imageNamed:@"bg.png"] forState:UIControlStateNormal];
// Set button title color for differenet state
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateFocused];
// Add click event on button
[button addTarget:self action:@selector(ClickedMethod:) forControlEvents:UIControlEventTouchUpInside];
// Finally, You can add button object in view
[self.view addSubview:button];
}
-(IBAction)ClickedMethod:(id)sender {
NSLog(@"Button Clicked");
}
No comments: