How to change the background color of a UIButton while it’s highlighted?

You can override UIButton‘s setHighlighted method. Objective-C – (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { self.backgroundColor = UIColorFromRGB(0x387038); } else { self.backgroundColor = UIColorFromRGB(0x5bb75b); } } Swift 3.0 and Swift 4.1 override open var isHighlighted: Bool { didSet { backgroundColor = isHighlighted ? UIColor.black : UIColor.white } }

How to create a button programmatically?

Here is a complete solution to add a UIButton programmatically with the targetAction. Swift 2.2 override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) button.backgroundColor = .greenColor() button.setTitle(“Test Button”, forState: .Normal) button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) self.view.addSubview(button) } func buttonAction(sender: UIButton!) { print(“Button tapped”) } It is … Read more

Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

I’m a little late to this party, but I think I have something useful to add. Kekoa’s answer is great but, as RonLugge mentions, it can make the button no longer respect sizeToFit or, more importantly, can cause the button to clip its content when it is intrinsically sized. Yikes! First, though, A brief explanation … Read more

Color Tint UIButton Image

As of iOS 7, there is a new method on UIImage to specify the rendering mode. Using the rendering mode UIImageRenderingModeAlwaysTemplate will allow the image color to be controlled by the button’s tint color. Objective-C UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *image = [[UIImage imageNamed:@”image_name”] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [button setImage:image forState:UIControlStateNormal]; button.tintColor = [UIColor redColor]; Swift let … Read more

UIButton remove all target-actions

Call removeTarget:action:forControlEvents:, pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents). Objective-C [someControl removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; Swift 2 button.removeTarget(nil, action: nil, forControlEvents: .AllEvents) Swift 3 or higher button.removeTarget(nil, action: nil, for: .allEvents)

How do I put the image on the right side of the text in a UIButton?

Simplest solution: iOS 10 & up, Swift: button.transform = CGAffineTransform(scaleX: -1.0, y: 1.0) button.titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0) button.imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0) Before iOS 10, Swift/Obj-C: button.transform = CGAffineTransformMakeScale(-1.0, 1.0); button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0); button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0); iOS 9 & up, Swift: (Recommended) button.semanticContentAttribute = .forceRightToLeft

How do you add multi-line text to a UIButton?

For iOS 6 and above, use the following to allow multiple lines: button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; // you probably want to center it button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to [button setTitle: @”Line1\nLine2″ forState: UIControlStateNormal]; For iOS 5 and below use the following to allow multiple lines: button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; // you probably want to … Read more

How can I set the title of a UIButton as left-aligned?

Set the contentHorizontalAlignment: // Swift emailBtn.contentHorizontalAlignment = .left; // Objective-C emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; You might also want to adjust the content left inset otherwise the text will touch the left border: // Swift 3 and up: emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0); // Objective-C emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

How do I create a basic UIButton programmatically?

Here’s one: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@”Show View” forState:UIControlStateNormal]; button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view addSubview:button];

tech