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.

uibutton
Filter by
Sorted by
Tagged with
571 votes
34 answers
567k views

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 ...
domlao's user avatar
  • 15.9k
523 votes
14 answers
241k views

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 ...
Madan Mohan's user avatar
  • 8,790
415 votes
19 answers
379k views

Set UIButton title UILabel font size programmatically

I need to set the font size of the title UILabel of a UIButton programmatically.
user avatar
391 votes
33 answers
208k views

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 ...
Owain Hunt's user avatar
  • 4,619
381 votes
35 answers
251k views

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 ...
jasongregori's user avatar
  • 11.6k
358 votes
6 answers
93k views

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 ...
Ken's user avatar
  • 31k
343 votes
22 answers
239k views

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 ...
Logan Shire's user avatar
  • 5,063
290 votes
19 answers
301k views

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's user avatar
  • 14.4k
282 votes
21 answers
269k views

Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

Historic question. Note that 15 years later, .imagePadding 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 ...
Justin Galzic's user avatar
279 votes
13 answers
342k views

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 ...
rocket101's user avatar
  • 7,447
278 votes
24 answers
413k views

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's user avatar
  • 3,120
275 votes
16 answers
395k views

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'...
GuiGui23's user avatar
  • 4,065
274 votes
32 answers
350k views

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 ...
MartinMoizard's user avatar
242 votes
24 answers
66k views

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 ...
exsulto's user avatar
  • 2,505
230 votes
11 answers
85k views

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 ...
Ben Packard's user avatar
  • 26.3k
217 votes
26 answers
137k views

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 *)...
rein's user avatar
  • 33.3k
214 votes
12 answers
318k views

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:@"...
Spanky's user avatar
  • 4,990
210 votes
19 answers
247k views

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 ...
adit's user avatar
  • 33.2k
209 votes
5 answers
187k views

How can I change UIButton title color?

I create a button programmatically.......... button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; [...
HelloWorld's user avatar
  • 7,236
201 votes
9 answers
116k views

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 ...
Olie's user avatar
  • 24.7k
198 votes
16 answers
222k views

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:...
double07's user avatar
  • 2,575
197 votes
36 answers
103k views

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 ...
Kevin Bomberry's user avatar
185 votes
5 answers
138k views

UIButton title text color

I'm setting text color for UIButton headingButton.titleLabel.textColor = [UIColor colorWithRed:36/255.0 green:71/255.0 ...
Ali's user avatar
  • 1,951
183 votes
35 answers
120k views

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 ...
NRaf's user avatar
  • 7,447
177 votes
15 answers
89k views

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 ...
user2665539's user avatar
  • 1,781
164 votes
19 answers
132k views

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;...
Andy A's user avatar
  • 4,241
164 votes
14 answers
96k views

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. ...
kbanman's user avatar
  • 4,223
161 votes
26 answers
137k views

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]; [...
reinaldoluckman's user avatar
160 votes
15 answers
132k views

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 ...
user478681's user avatar
  • 8,330
160 votes
7 answers
104k views

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

I'm creating a colored image like this: CGRect rect = CGRectMake(0, 0, 1, 1); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); ...
Thorsten's user avatar
  • 13.1k
159 votes
23 answers
372k views

Make a UIButton programmatically in Swift

I am trying to build UIs programmatically with Swift. How can I get this action working? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...
Benr783's user avatar
  • 2,880
157 votes
12 answers
174k views

UIButton Image + Text IOS

I need a UIButton with image & text. Image should be in the top & text comes under the image both should be clickable.
Codesen's user avatar
  • 7,782
153 votes
18 answers
105k views

Underlining text in UIButton

Can anyone suggest how to underline the title of a UIButton ? I have a UIButton of Custom type, and I want the Title to be underlined, but the Interface Builder does not provide any option to do so. ...
RVN's user avatar
  • 4,167
153 votes
11 answers
217k views

How to set the title text color of UIButton?

I tried changing the colors of the text for a button, but it's still staying white. isbeauty = UIButton() isbeauty.setTitle("Buy", forState: UIControlState.Normal) isbeauty.titleLabel?.textColor = ...
coolmac's user avatar
  • 1,573
152 votes
15 answers
139k views

How to make UIButton's text alignment center? Using IB

I can't set the title of UIButton using IB as center. My title is multi line.It is giving like this one But I want like this one I have given space in this but I don't want to do that. As it is ...
Inder Kumar Rathore's user avatar
149 votes
17 answers
187k views

iOS: UIButton resize according to text length

In interface builder, holding Command + = will resize a button to fit its text. I was wondering if this was possible to do programmatically before the button was added to the view. UIButton *button =...
Oh Danny Boy's user avatar
  • 4,887
146 votes
18 answers
232k views

Get button click inside UITableViewCell

I have a view controller with a table view and a separate nib for the table cell template. The cell template has some buttons. I want to access the button click along with the index of the cell ...
ankit_rck's user avatar
  • 1,796
142 votes
12 answers
331k views

How to change UIButton image in Swift

I am trying to change the image of a UIButton using Swift... What should I do This is OBJ-C code.but I don't know with Swift: [playButton setImage:[UIImage imageNamed:@"play.png"] forState:...
Lop Hu's user avatar
  • 1,473
137 votes
16 answers
151k views

How to make a simple rounded button in Storyboard?

I just started learning iOS development, cannot find how to make simple rounded button. I find resources for old versions. Do I need to set a custom background for a button? In Android, I would just ...
Gintas_'s user avatar
  • 5,000
133 votes
9 answers
90k views

Using Tint color on UIImageView

I have my own subclass of UIButton. I add UIImageView on it and add an image. I would like to paint it over the image with a tint color but it doesn't work. So far I have: - (id)initWithFrame:(...
Marko Zadravec's user avatar
127 votes
5 answers
58k views

Is it possible to adjust x,y position for titleLabel of UIButton?

Is it possible to adjust the x,y position for the titleLabel of a UIButton? Here is my code: UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setFrame:CGRectMake(0....
RAGOpoR's user avatar
  • 8,118
126 votes
14 answers
172k views

Attach parameter to button.addTarget action in Swift

I am trying to pass an extra parameter to the buttonClicked action, but cannot work out what the syntax should be in Swift. button.addTarget(self, action: "buttonClicked:", forControlEvents: ...
iphaaw's user avatar
  • 6,974
124 votes
16 answers
91k views

iPhone UIButton - image position

I have a UIButton with text "Explore the app" and UIImage (>) In Interface Builder it looks like: [ (>) Explore the app ] But I need to place this UIImage AFTER the text: [ Explore the app (>)...
Pavel Yakimenko's user avatar
118 votes
8 answers
111k views

Get position of UIView in respect to its superview's superview

I have a UIView, in which I have arranged UIButtons. I want to find the positions of those UIButtons. I am aware that buttons.frame will give me the positions, but it will give me positions only ...
Shailesh's user avatar
  • 3,100
116 votes
11 answers
107k views

Swift - UIButton with two lines of text

I was wondering if it is possible to create a UIButton with two lines of text. I need each line to have a different font size. The first line will be 17 point and the second will be 11 point. I've ...
Scott's user avatar
  • 1,378
113 votes
17 answers
130k views

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: ...
KONG's user avatar
  • 7,291
113 votes
11 answers
144k views

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 ...
baranbaris's user avatar
  • 1,291
112 votes
21 answers
73k views

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 ...
marty's user avatar
  • 1,137
108 votes
10 answers
207k views

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 ...
aggiesfan64's user avatar
  • 1,133
108 votes
15 answers
128k views

Problems with corner radius and drop shadow for button

I'm trying to create a button with rounded corners and a drop shadow. No matter how I switch up, the button will not display correctly. I've tried masksToBounds = false and masksToBounds = true, but ...
Jake's user avatar
  • 13.5k

1
2 3 4 5
226