Questions tagged [uibutton]

UIButton is a subclass of UIView for displaying buttons in iOS. It implements the target action paradigm to send events to the controller layer.

A UIButton intercepts touch events and sends an action message to a target object when tapped. Methods for setting the target and action are inherited from UIControl (). This class provides methods for setting the title, image, and other appearance properties of a UIButton. By using these accessors, you can specify a different appearance for each button state:

  • UIControlStateNormal,
  • UIControlStateHighlighted,
  • UIControlStateDisabled,
  • UIControlStateSelected.
  • UIControlStateFocused.

Resources:

11252 questions
571
votes
34 answers

How do I create a basic UIButton programmatically?

How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButtons will be created dynamically and its layout or properties are set.
domlao
  • 15,663
  • 34
  • 95
  • 134
521
votes
14 answers

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

I need to display an email address on the left side of a UIButton, but it is being positioned to the centre. Is there any way to set the alignment to the left side of a UIButton? This is my current code: UIButton* emailBtn = [[UIButton alloc]…
Madan Mohan
  • 8,764
  • 17
  • 62
  • 96
414
votes
19 answers

Set UIButton title UILabel font size programmatically

I need to set the font size of the title UILabel of a UIButton programmatically.
syam s
391
votes
33 answers

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

I have the following code... UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds]; buttonLabel.text = @"Long text string"; [targetButton addSubview:buttonLabel]; [targetButton bringSubviewToFront:buttonLabel]; ...the idea…
Owain Hunt
  • 4,619
  • 2
  • 20
  • 10
378
votes
35 answers

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

I don't want to use a subview if I can avoid it. I want a UIButton with a background image, text, and an image in it. Right now, when I do that, the image is on the left side of the text. The background image, text, and image all have different…
jasongregori
  • 11,451
  • 5
  • 43
  • 41
357
votes
6 answers

UIButton remove all target-actions

I have added multiple target-action-forControlEvents: to a UIButton. I'd like to remove all of these in one go without deallocating anything. I will then set new targets. Is this possible and how do I go about it?
Ken
  • 30,811
  • 34
  • 116
  • 155
339
votes
22 answers

Color Tint UIButton Image

I noticed that when I place a white or black UIImage into a UISegmentedControl it automatically color masks it to match the tint of the segmented control. I thought this was really cool, and was wondering if I could do this elsewhere as well. For…
Logan Shire
  • 5,013
  • 4
  • 29
  • 37
283
votes
19 answers

How to change font of UIButton with Swift

I am trying to change the font of a UIButton using Swift... myButton.font = UIFont(name: "...", 10) However .font is deprecated and I'm not sure how to change the font otherwise. Any suggestions?
Stephen Fox
  • 14,190
  • 19
  • 48
  • 52
280
votes
21 answers

Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

Historic question. Note that 15 years later, .padding is the space between the image and label. Original question: I would like to place an icon left of the two lines of text such that there's about 2-3 pixels of space between the image and the…
Justin Galzic
  • 3,951
  • 6
  • 28
  • 39
278
votes
24 answers

How to create a button programmatically?

How do I programmatically create graphical elements (like a UIButton) in Swift? I tried to create and add button into a view, but wasn't able to.
val_lek
  • 3,120
  • 3
  • 14
  • 12
274
votes
12 answers

Changing text of UIButton programmatically swift

Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here's what I have: currencySelector.text = "foobar" Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I…
rocket101
  • 7,369
  • 11
  • 45
  • 64
274
votes
32 answers

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

At some point in my app I have a highlighted UIButton (for example when a user has his finger on the button) and I need to change the background color while the button is highlighted (so while the finger of the user is still on the button). I tried…
MartinMoizard
  • 6,600
  • 12
  • 45
  • 75
273
votes
16 answers

How can I make a button have a rounded border in Swift?

I'm building an app using swift in the latest version of Xcode 6, and would like to know how I can modify my button so that it can have a rounded border that I could adjust myself if needed. Once that's done, how can I change the color of the border…
GuiGui23
  • 4,035
  • 4
  • 19
  • 17
239
votes
24 answers

How to stop unwanted UIButton animation on title change?

In iOS 7 my UIButton titles are animating in and out at the wrong time - late. This problem does not appear on iOS 6. I'm just using: [self setTitle:text forState:UIControlStateNormal]; I would prefer this happens instantly and without a blank…
exsulto
  • 2,475
  • 2
  • 15
  • 10
228
votes
11 answers

Autolayout - intrinsic size of UIButton does not include title insets

If I have a UIButton arranged using autolayout, its size adjusts nicely to fit its content. If I set an image as button.image, the instrinsic size again seems to account for this. However, if I tweak the titleEdgeInsets of the button, the layout…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
1
2 3
99 100