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
217
votes
26 answers

Detecting which UIButton was pressed in a UITableView

I have a UITableView with 5 UITableViewCells. Each cell contains a UIButton which is set up as follows: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier =…
rein
  • 32,967
  • 23
  • 82
  • 106
213
votes
12 answers

Setting an image for a UIButton in code

How do you set the image for a UIButton in code? I have this: UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnTwo.frame = CGRectMake(40, 140, 240, 30); [btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal]; [btnTwo…
Spanky
  • 4,980
  • 7
  • 36
  • 37
208
votes
5 answers

How can I change UIButton title color?

I create a button programmatically.......... button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; [button setTitle:@"Show View"…
HelloWorld
  • 7,156
  • 6
  • 39
  • 36
204
votes
19 answers

How to adjust an UIButton's imageSize?

How can I adjust the image size of the UIButton? I am setting the image like this: [myLikesButton setImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal]; However this fills up the image to the full button, how do I make the…
adit
  • 32,574
  • 72
  • 229
  • 373
198
votes
9 answers

how to programmatically fake a touch event to a UIButton?

I'm writing some unit tests and, because of the nature of this particular app, it's important that I get as high up the UI chain as possible. So, what I'd like to do is programmatically trigger a button-press, as if the user had pressed the button…
Olie
  • 24,597
  • 18
  • 99
  • 131
197
votes
16 answers

How to round the corners of a button

I have a rectangle image (jpg) and want to use it to fill the background of a button with rounded corner in xcode. I wrote the following: UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; CGRect frame = CGRectMake(x, y,…
double07
  • 2,565
  • 2
  • 22
  • 22
196
votes
36 answers

UIButton: Making the hit area larger than the default hit area

I have a question dealing with UIButton and its hit area. I am using the Info Dark button in interface builder, but I am finding that the hit area is not large enough for some people's fingers. Is there a way to increase the hit area of a button…
Kevin Bomberry
  • 3,122
  • 4
  • 22
  • 18
185
votes
5 answers

UIButton title text color

I'm setting text color for UIButton headingButton.titleLabel.textColor = [UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 …
Ali
  • 1,951
  • 2
  • 14
  • 14
182
votes
35 answers

Label under image in UIButton

I'm trying to create a button which has some text beneath the icon (sorta like the app buttons) however it seems to be quite difficult to achieve. Any ideas how can I go about get the text to display below the image with a UIButton?
NRaf
  • 7,407
  • 13
  • 52
  • 91
176
votes
15 answers

Setting UIButton image results in blue button in iOS 7

On iOS 6 SDK I wrote the following lines of code to display an image inside a button: NSURL *thumbURL2 = [NSURL URLWithString:@"http://example.com/thumbs/2.jpg"]; NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2]; UIImage *thumb2 =…
user2665539
  • 1,771
  • 2
  • 11
  • 3
164
votes
14 answers

How to disable the highlight control state of a UIButton?

I've got a UIButton that, when selected, shouldn't change state when being touched. The default behaviour is for it to be in UIControlStateHighlighted while being touched, and this is making me angry. Suggestions?
kbanman
  • 4,223
  • 6
  • 32
  • 40
163
votes
19 answers

Disabled UIButton not faded or grey

In my iPhone app, I have a UIButton which I have created in Interface Builder. I can successfully enable and disable it like this in my code ... sendButton.enabled = YES; or sendButton.enabled = NO; However, the visual look of the button is always…
Andy A
  • 4,191
  • 7
  • 38
  • 56
160
votes
7 answers

Creating a UIImage from a UIColor to use as a background image for UIButton

I'm creating a colored image like this: CGRect rect = CGRectMake(0, 0, 1, 1); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, …
Thorsten
  • 12,921
  • 17
  • 60
  • 79
160
votes
15 answers

UIButton: set image for selected-highlighted state

I set an images for button's states Normal,Highlighted and Selected, but when the button in selected state and I press/highlight it I didn't see my highlighted image but just grayed picture. Is it possible to set an image for highlighted state when…
user478681
  • 8,330
  • 4
  • 26
  • 34
160
votes
26 answers

UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?

If I put only an image in a button and set the imageEdgeInsets more close to the top, the image stays centered and all works as expected: [button setImage:image forState:UIControlStateNormal]; [button setImageEdgeInsets:UIEdgeInsetsMake(-15.0, 0.0,…
reinaldoluckman
  • 6,317
  • 8
  • 42
  • 50