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
113
votes
17 answers

scale Image in an UIButton to AspectFit?

I want to add an image to a UIButton, and also want to scale my image to fit with the UIButton (make image smaller). Please show me how to do it. This is what I have tried, but it does't work: Adding image to button and using…
KONG
  • 7,271
  • 6
  • 28
  • 27
112
votes
21 answers

UIButton won't go to Aspect Fit in iPhone

I have a couple UIButtons, and in IB they're set to Aspect Fit, but for some reason they're always stretching. Is there something else you have to set? I tried all the different view modes and none of them work, they all stretch.
marty
  • 1,137
  • 2
  • 7
  • 7
112
votes
11 answers

Disable a Button

I want to disable a button (UIButton) on iOS after it is clicked. I am new to developing for iOS but I think the equivalent code on objective - C is this: button.enabled = NO; But I couldn't do that on swift.
baranbaris
  • 1,281
  • 2
  • 8
  • 12
108
votes
10 answers

How do you add an action to a button programmatically in xcode

I know how to add an IBAction to a button by dragging from the interface builder, but I want to add the action programmatically to save time and to avoid switching back and forth constantly. The solution is probably really simple, but I just can't…
aggiesfan64
  • 1,133
  • 2
  • 7
  • 8
107
votes
17 answers

Is it possible to change a UIButtons background color?

This one has me stumped. Is it possible at all to change the background color of a UIButton in Cocoa for iPhone. I've tried setting the background color but it only changes the corners. setBackgroundColor: seems to be the only method available for…
dubbeat
  • 7,706
  • 18
  • 70
  • 122
105
votes
7 answers

Changing UIButton text

So I'm trying to update the text on a UIButton when I click it. I'm using the following line to change the text: calibrationButton.titleLabel.text = @"Calibration"; I have verified that the text is changing, but when I run the app and I click on…
Julian Coltea
  • 3,599
  • 10
  • 26
  • 32
101
votes
10 answers

Interaction beyond bounds of UIView

Is it possible for a UIButton (or any other control for that matter) to receive touch events when the UIButton's frame lies outside of it's parent's frame? Cause when I try this, my UIButton doesn't seem to be able to receive any events. How do I…
ThomasM
  • 2,647
  • 3
  • 25
  • 30
101
votes
9 answers

Gesture recognizer and button actions

I have a view hierarchy that looks something like this: UIView (A) UIView > UIImageView UIView > UIView (B) UIView > UIView (B) > Rounded Rect Button UIView > UIView (B) > UIImageView UIView > UIView (B) > UILabel I've attached gesture…
Mustafa
  • 20,504
  • 42
  • 146
  • 209
100
votes
14 answers

How can I increase the Tap Area for UIButton?

I use UIButton with auto layout. When images are small the tap area is also small. I could imagine several approaches to fix this: increase the image size, i.e., place a transparent area around the image. This is not good because when you position…
Sven Bauer
  • 1,619
  • 2
  • 13
  • 22
100
votes
12 answers

is it possible to update UIButton title/text programmatically?

I have a UIButton, that when pressed, brings up a new view where the user can change some settings. When the view is dismissed, I'd like to update the title/text of the UIButton to reflect the new state. I'm calling: [myButton setTitle: @"myTitle"…
George Armhold
  • 30,824
  • 50
  • 153
  • 232
99
votes
17 answers

UIButton doesn't listen to content mode setting?

firstButton is a UIButton of type Custom. I'm programmatically putting three of them across each cell of a table, thusly: [firstButton setImage:markImage forState:UIControlStateNormal]; [firstButton…
Dan Ray
  • 21,623
  • 6
  • 63
  • 87
93
votes
9 answers

How to prevent UIButton from flashing when updating the title

When I call setTitle on a UIButton, the button flashes in iOS 7. I tried setting myButton.highlighted = NO, but that didn't stop the button from flashing. [myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"]…
FierceMonkey
  • 1,964
  • 1
  • 16
  • 22
91
votes
9 answers

Keeping a UIButton selected after a touch

After my user clicks a button, I'd like that button to stay pushed during the time that I perform a network operation. When the network operation is complete, I want the button to return to its default state. I've tried calling -[UIButton…
Greg Maletic
  • 6,225
  • 8
  • 54
  • 73
90
votes
6 answers

How to change Button Title Alignment in Swift?

I am trying to set the title on a Button to left. But everything i tried is not working. With: UILabelButton.titleLabel.textAlignment = .Left Or: UILabelButton.contentVerticalAlignment = UIControlContentVerticalAlignment // There is no left The…
derdida
  • 14,784
  • 16
  • 90
  • 139
89
votes
6 answers

UIButton with two lines of text in the title (numberOfLines=2)

I'm trying to make a UIButton that has two lines of text in its titleLabel. This is the code I'm using: UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)]; titleButton.titleLabel.font = [UIFont…
marketer
  • 41,507
  • 11
  • 37
  • 40