×

iOS UIButton


What is UIButton ?

UIButton is used to design a button in iPhone Development. It may be custom or default. There are various types of Button available by Default like ...

UIButtonTypeCustom
UIButtonTypeRoundedRect
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
UIButtonTypeContactAdd 

Important Methods Mostly Used in UIButton are as follows :-

1) UIButton's setTitle property is used to give title to a Button

[button setTitle:@"ButtonTitle" forState:UIControlStateNrmal]

2) UIButton's setTitleColor property is used to change text Color of a Button

[button setTitleColor:[UIColor  redColor] forState:UIControlStateNrmal]

3) UIButton's titleLabel.font property is used to give font to a title of a Button

button.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0]

4) UIButton's addtarget property is used to give target to a Button. When user clicks on a button addTarget property performs user Defined Action.

[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]

-(void)buttonClicked
{
NSLog(@"Button Clicked");
}

How to create UIButton with default Types available ?

With the help of buttonWithType Method You can see suggesstions based on UIButton's Types.

UIButton *roundRectButton = [UIButton buttonWithType:
                                 UIButtonTypeRoundedRect];
[roundRectButton setFrame:CGRectMake(60, 50, 200, 40)];
// sets title for the button
[roundRectButton setTitle:@"Rounded Button" forState:UIControlStateNormal];
[self.view addSubview:roundRectButton];