UIButton
UIButton : UIControl intercepts touch events and sends an action message to a target object when it’s tapped. You can set the title, image, and other appearance properties of a button. In addition, you can specify a different appearance for each button state.
Attaching a Method to a Button
Section titled “Attaching a Method to a Button”To add a method to a button, first create an action method:
Objective-C
-(void) someButtonAction{ NSLog(@"Button is tapped");
}Swift
func someButtonAction() { print("Button is tapped") }Now to add this action method to your button, you have to write following line of code:
Objective C
[yourButtonInstance addTarget:self action:@selector(someButtonAction) forControlEvents:UIControlEventTouchUpInside];Swift
yourButtonInstance.addTarget(self, action: #selector(someButtonAction), forControlEvents: .touchUpInside)For ControlEvents, all members of ENUM UIControlEvents are valid.
Creating a UIButton
Section titled “Creating a UIButton”UIButtons can be initialized in a frame:
Swift
let button = UIButton(frame: CGRect(x: x, y: y, width: width, height: height)Objective C
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];A specific type of UIButton can be created like this:
Swift
let button = UIButton(type: .Custom)Objective C
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];where type is a UIButtonType:
enum UIButtonType : Int { case Custom case System case DetailDisclosure case InfoLight case InfoDark case ContactAdd static var RoundedRect: UIButtonType { get }}Set title
Section titled “Set title”Swift
button.setTitle(titleString, forState: controlState)Objective C
[button setTitle:(NSString *) forState:(UIControlState)];To set the default title to “Hello, World!”
Swift
button.setTitle("Hello, World!", forState: .normal)Objective C
[button setTitle:@"Hello, World!" forControlState:UIControlStateNormal];Set title color
Section titled “Set title color”//Swiftbutton.setTitleColor(color, forControlState: controlState)
//Objective-C[button setTitleColor:(nullable UIColor *) forState:(UIControlState)];To set the title color to blue
//Swiftbutton.setTitleColor(.blue, for: .normal)
//Objective-C[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]Horizontally aligning contents
Section titled “Horizontally aligning contents”Swift
//Align contents to the left of the framebutton.contentHorizontalAlignment = .left
//Align contents to the right of the framebutton.contentHorizontalAlignment = .right
//Align contents to the center of the framebutton.contentHorizontalAlignment = .center
//Make contents fill the framebutton.contentHorizontalAlignment = .fillObjective C
//Align contents to the leftbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//Align contents to the rightbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//Align contents to the centerbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//Align contents to fill the framebutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;Getting the title label
Section titled “Getting the title label”The underlying title label, if one exists, can be fetched using
Swift
var label: UILabel? = button.titleLabelObjective C
UILabel *label = button.titleLabel;This can be used to set the font of the title label, for example
Swift
button.titleLabel?.font = UIFont.boldSystemFontOfSize(12)Objective C
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];Disabling a UIButton
Section titled “Disabling a UIButton”A button can be disabled by
Swift
myButton.isEnabled = falseObjective-C:
myButton.enabled = NO;The button will become gray:
If you don’t want the button appearance to change when disabled set adjustsImageWhenDisabled to false / NO
Adding an action to an UIButton via Code (programmatically)
Section titled “Adding an action to an UIButton via Code (programmatically)”To add a method to a button, first create an action method:
Objective-C
-(void)someButtonAction:(id)sender { // sender is the object that was tapped, in this case its the button. NSLog(@"Button is tapped");}Swift
func someButtonAction() { print("Button is tapped")}Now to add this action method to your button, you have to write following line of code:
Objective C
[yourButtonInstance addTarget:self action:@selector(someButtonAction) forControlEvents:UIControlEventTouchUpInside];Swift
yourButtonInstance.addTarget(self, action: #selector(someButtonAction), forControlEvents: .TouchUpInside)For ControlEvents parameter, all members of ENUM UIControlEvents are valid.
Setting Font
Section titled “Setting Font”Swift
myButton.titleLabel?.font = UIFont(name: "YourFontName", size: 20)Objective C
myButton.titleLabel.font = [UIFont fontWithName:@"YourFontName" size:20];Get UIButton’s size strictly based on its text and font
Section titled “Get UIButton’s size strictly based on its text and font”To get the the exact size of a UIButton’s text based on its font, use the function intrinsicContentSize.
Swift
button.intrinsicContentSize.widthObjective-C
button.intrinsicContentSize.width;Set Image
Section titled “Set Image”button.setImage(UIImage(named:"test-image"), forState: .normal)Objective C
Section titled “Objective C”[self.button setImage:[UIImage imageNamed:@"test-image"] forState:UIControlStateNormal];Multiple Control States
Section titled “Multiple Control States”You can also set an image for multiple UIControlStates, for example to set the same image for the Selected and Highlighted state:
button.setImage(UIImage(named:"test-image"), forState:[.selected, .highlighted])Objective C
Section titled “Objective C”[self.button setImage:[UIImage imageNamed:@"test-image"] forState:UIControlStateSelected|UIControlStateHighlighted];Remarks
Section titled “Remarks”Button Types
Section titled “Button Types”A button’s type defines its basic appearance and behavior. After creating a button, you cannot change its type. The most commonly used button types are the Custom and System types, but use the other types when appropriate
No button style.A system style button, such as those shown in navigation bars and toolbars.A detail disclosure button.An information button that has a light background.An information button that has a dark background.A contact add button.When creating a custom button—that is a button with the type custom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
