Hit area is only on label of UIButton - iphone

I create a UIButton like this:
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,100,100);
The button works, but the hit area is only on the text, not the entire button. I've set the background of the button to be [UIColor clearColor]. What do I need to do to make it so that the entire frame of the button is the hit area?
I suppose I could change the frame of the label to equal the button, but that seems hokey. Is there a better way?

try like this,
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0,0,100,100);
button.backgroundColor=[UIColor clearColor];
[button setTitle:#"Set" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:button];

The button in question was being built up with layers. What I ended up doing was ensuring the background layer had a solid background color. Simply setting the background of the button to clear did not work in this case.
//set a background color so it is clickable
[layer0 setBackgroundColor: CGCOLORA(0, 1) ];

Setting the opaque property of the UIButton to true worked for me (iOS10)

Related

UIButton setTitleColor and setImage not working

I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototype cells. Inside the prototype cell, I have a UIButton (rounded rectangle) hooked up to a sub-classed UIButton class. The tableview gets its data from an array, and the data is printed out in cellForRowAtIndexPath.
Inside cellForRowAtIndexPath, I conditionally modify the cell's appearance. This is when I start getting problems. I am able to successfully change the text of the button. However, if I attempt to change the image of the button, using:
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
the image is changed, but the text disappears.
If I attempt to change the text color of the button, using:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
Nothing happens whatsoever. The color remains the same (unless I also try to change the image, in which case it completely disappears).
I am sensing I am doing something fundamentally wrong here -- do you have any suggestions?
use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
to change image with text left
use
btn.titleLabel.textColor = yourcolor;
to change text color
Define myButton as below and check.This may be the wrong in your code.The code is:
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
This is newly editted part:
[myButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[myButton setTitleColor:yourcolor forState:UIControlStateNormal];
This is the way to set different properties to a Custom Button...You also find that setBackgroudimage and setTitleColor properties here...
UIButtin *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(165, 205, 65, 40)];
[myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal];
[myButton setTitle:#"Hi....." forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
[cell addSubview:myButton];
Thank u..
I guess what you want is to change your button background:
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Your text disappears because you have set the image at button front side as button main image.
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
You need to set the button background image so that your text would be visible as the image will be drawn at the rear side of your UIButton context.
So just replace the above line of code to :
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];

transparent UIButton in iOS 5 sdk

What is the best way to achive having test showing up as normal color but the Rect button lines being not shown effectively making it transparent.
i have tried alpha settings but no luck as it makes the whole button AND text transparent, which is not what i want i would prefer to achieve this by UIButton rather then Image.
with image i struggled to link actions / outlets among views and found it easier to connect using cntrl button and linking with views.
Any working idea would be good
Try creating a custom button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, w, h);
[btn addTarget:self action:#selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Push me!" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
[btn setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
[self.view addSubview:btn];

How to make transparent UIButton in UITableView Cell

When My .Xib File Is Loaded That Time UITableViewCell is Fill Up With Black Background Color,For That, I am using
cell.contentView.backGroundColor = [UIColor blackColor]
But In UITableViewCell One UIButton With "Custom" Button Type Is Generated Programatically
so the area Cover By UIButton is a Not transparent (Or Cell background - black Color is Not Display), it is a white color,,,
I Want To Make UIButton With Transparent Background
SO How I Can ?
UIButton *button;
button.backgroundColor = [UIColor clearColor];
try this one it work for me
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 5.0, 160.0, 40.0);
[button setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:button];
Why don't you make an image of button along with text you want to show and add this as button background with button type custom.

Simple Text UIButton Highlighting

I've got a UIButton that I create at runtime. I put a title on it and have a few states set for it. I DO NOT have an image set to the background and want to avoid this. So the button should simply be a custom button with text in the middle.
When I click on it I see 0 feedback that I clicked on it. I can set the text to change states, but I don't get that grey highlighted color on top.
I've tried:
[button setShowsTouchWhenHighlighted:YES];
And get nothing. again I can set the title color to chagne based on the state but thats not really what I'm looking for. Any Ideas?
Thanks for any and all help.
change button type as:
UIButtonTypeRoundedRect
instead of UIButtonTypeCustom
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
[myButton setTitle:#"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[superView addSubview:myButton];

UIButton of type UIButtonTypeCustom will not display Title (iPhone)

I must have overlooked something completely obvious?? but my button displays its image and size correctly, but I simply can't get the Title to show up.
I did a really simple test, the Title does not even show up when I do this:
CGRect frameBtn = CGRectMake(160.0f, 150.0f, 144.0f, 42.0f);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"left_halfscreen_button.png"] forState:UIControlStateNormal];
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button setFrame:frameBtn];
NSLog(#"Title:%#", [button currentTitle]);
//prints "Title:Hello
[self addSubview:button];
I have a factory class that generates custom buttons for me and I thought I messed some detail up there, so I moved the above code directly into my UIView, the title is still blank.
Is this a bug or am I simply missing something right in front of my eyes.
Thank you for the extra set of eyes:)
Image overrides title, you need to make the image a background image to show the title.
[button setBackgroundImage:[UIImage imageNamed:#"left_halfscreen_button.png"]
forState:UIControlStateNormal];
I just hab spome quite similar problem... Just set the title color, I guess the current one is white ;)
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
I found the problem!
It's not Image overriding the title. It is being push off frame by the image.
Try use this:
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -img.size.width, 0.0, 0.0 )];
Hope it helps!
When you use UIButton, button uses some built-in size for its frame.
You should set the frame:
button.frame = CGRectMake(0, 0, width, height);
or even better and comfortable:
[button sizeToFit];
I had a similar problem that the title was not shown. I forgot to set the frame property:
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);