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.
11,266
questions
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 ...
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 ...
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.
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 ...
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 ...
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 ...
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 ...
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?
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 ...
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 ...
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.
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'...
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 ...
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 ...
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 ...
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 *)...
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:@"...
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 ...
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];
[...
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 ...
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:...
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 ...
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
...
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 ...
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 ...
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;...
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.
...
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];
[...
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 ...
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();
...
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, ...
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.
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.
...
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 = ...
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 ...
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 =...
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 ...
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:...
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 ...
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:(...
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....
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: ...
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 (>)...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...