Tutorial by Examples

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 but...
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:@&quot...
//Swift button.setTitleColor(color, forControlState: controlState) //Objective-C [button setTitleColor:(nullable UIColor *) forState:(UIControlState)]; To set the title color to blue //Swift button.setTitleColor(.blue, for: .normal) //Objective-C [button setTitleColor:[UIColor blueColo...
Swift //Align contents to the left of the frame button.contentHorizontalAlignment = .left //Align contents to the right of the frame button.contentHorizontalAlignment = .right //Align contents to the center of the frame button.contentHorizontalAlignment = .center //Make contents fill th...
The underlying title label, if one exists, can be fetched using Swift var label: UILabel? = button.titleLabel Objective 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) ...
A button can be disabled by Swift myButton.isEnabled = false Objective-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
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...
Swift myButton.titleLabel?.font = UIFont(name: "YourFontName", size: 20) Objective C myButton.titleLabel.font = [UIFont fontWithName:@"YourFontName" size:20];
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 wri...
To get the the exact size of a UIButton's text based on its font, use the function intrinsicContentSize. Swift button.intrinsicContentSize.width Objective-C button.intrinsicContentSize.width;
Swift button.setImage(UIImage(named:"test-image"), forState: .normal) Objective C [self.button setImage:[UIImage imageNamed:@"test-image"] forState:UIControlStateNormal]; Multiple Control States You can also set an image for multiple UIControlStates, for example to set...

Page 1 of 1