Add subview to UIButton - iphone

I'm trying to add subviews to a UIButton. This is working fine right now. But the button isn't clickable anymore as soon as I add the subviews.
I use the following code:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button addSubview:asyncImage];
[button addSubview:price];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
The button works again if I remove the 2 addSubview: methods. If anyone knows how to fix this it would be great!

I found a quick solutions. I needed to set the asyncImageView to the following:
asyncImage.userInteractionEnabled = NO;
asyncImage.exclusiveTouch = NO;
After this, it worked!

try:
[UIButton buttonWithType:UIButtonTypeCustom];
instead of:
[UIButton buttonWithType:UIButtonControlType];

The important thing here is to make sure that userInteractionEnabled will be set to NO. Fortunately it works immediately for UIImageView and UILabel (maybe for other subclasses of a UIView but those are the most popular subviews added to button) because by default for this classes it is set to NO by default. Unfortunately it is set to YES in UIView so make sure to change it in that case. Messing around with any other flags shouldn't be necessary. The nature of problem is that many people do not know that default value of this flag is different in subclasses.

Have you tried to put:
[asyncImage setUserInteractionEnabled:YES];

in the same sitiation i make this action:
inherit from UIButton and add all labels and imageview's of button to self, finally put new button to view as last subview and add targets of self button to this last button(also set backgroundColor to clearColor for transparent). now it will be clickable and works fine.

In Swift, test that is you have your UIButton blocked
uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false

I added a label to the subview button.
For a very long time I was looking for why my text in the button is not clickable. Solution in this thread:
myLable.isUserInteractionEnabled = false

Related

Disable UISlider from sliding, but still allow it to receive UIControlEventTouchDown

Is there a way to disable a UISlider from sliding, but to get its UIControlEventTouchDown to fire? One way it could be done I believe is to set the slider start value the same as the end value, but I'd like to avoid doing that. Does anyone know a better way?
I would suggest setting userInteractionEnabled = NO for your UISlider, and adding an invisible button to handle the TouchDown
UIButton *btnSlider = [UIButton buttonWithType:UIButtonTypeCustom];
btnSlider.frame = CGRectMake(slider.frame.origin.x, slider.frame.origin.y, slider.frame.size.width, slider.frame.size.height);
[btnSlider addTarget:self action:#selector(sliderTouched) forControlEvents:UIControlEventTouchDown];
btnSlider.backgroundColor = [UIColor clearColor];
[myView addSubview:btnSlider];
Then based on whatever condition you want to make the slider enabled, you can enable it and disable the button.

how to hide or remove UIbutton from headerview

I have a button in headerview of my tableview, its woking fine as well, but problem i am having is: When I want to hide that button on click of another button. How can I achieve this? I have already tried
[deletebutton removeFromSuperview];
where deleteButton is my buttons name.
Here is what I am doing
-(void)PutTableinEditMode{
DeleteButton=[UIButton buttonWithType:UIButtonTypeCustom];
DeleteButton.frame=CGRectMake(10,1, 65, 25);
[DeleteButton setTitle:#"Delete" forState:UIControlStateNormal];
DeleteButton.backgroundColor=[UIColor clearColor];
[DeleteButton addTarget:self action:#selector(DeleteMultipleToDos) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:DeleteButton];
}
Then I m having another function as below from where i want to hide this DeleteButton
-(void)DoneEditing
{
DeleteButton.hidden=YES;
[DeleteButton removeFromSuperview];
}
deletebutton.hidden = YES
if any of those two solution (removeFromSuperview and hidden) do not work, then you certainly don't have the right reference to your button. I guess your using nib files to define your views ? Then be sure the member deleteButton is well "linked" to the actual button. (i.e. verify using debug that your deleteButton var is not null)
Can u try and change the Text of that button because i'm thinking your sending messages to nil objects.
I got the it working actually [DeleteButton removeFromSuperView] is working .. problem was that the first method PutTableinEditMode was being called from somewhere else also, so i simply took a bool variable and on basis of its value added and removed the delete button
:)
Thanks guys for helping though :)
Ifeel so stupid to make such a silly mistake :)

Hit target on UIToolbar with custom-image buttons is not correct

I have a UIToolbar that I've customized with my own background image. Consequently, the built-in UIBarButtonItem appearance doesn't work for me, so I'm using images that are already prepared to show in the bar. I create a custom button item with this method:
+ (UIBarButtonItem *)customWithImage:(UIImage *)image enabled:(BOOL)enabled target:(id)target action:(SEL)selector {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//I've tried different values for the frame here, but no luck
button.frame = CGRectMake(0, 0, 44, 44);
button.enabled = enabled;
button.showsTouchWhenHighlighted = YES;
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
UIBarButtonItem *it = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
//Tried changing this, to no avail
it.width = 32.f;
return it;
I have one button on the left and one on the right and I'm trying to make it so that if you tap on the far left or far right of the UIToolbar, the corresponding button is tapped. However, with these custom buttons, the hit targets do not extend all the way to the edges of the UIToolbar, but are inset from the sides:
http://skitch.com/andpoul/d1p8g/hit-targets
Any help greatly appreciated!
UIBarButtonItem.width might be ignored if you're using a custom view (it probably just uses the width of the view).
A lame hack is to make the toolbar wider (so it sticks outside the screen) and add transparent edges to the background image to compensate. This brings the buttons closer to the edge of the screen.
An alternative is just to use a UIImageView with UIButton subviews.
I think the only way you will have to go is to make buttons wider (change image by adding some from left for one and right for another) and adjust size...

UIButton only responds in a small area

I'm trying to add a UIButton to a UIView, but am having some trouble with getting it to respond to touches.
I have a method which returns UIButtons after I provide it with a tag:
- (UIButton*)niceSizeButtonWithTag:(int)tag {
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:tag];
[aButton addTarget:self action:#selector(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
CGRect newFrame = aButton.frame;
newFrame.size.width = 44;
newFrame.size.height = 44;
[aButton setFrame:newFrame];
return aButton;
}
As you can see I'm creating a new button and increasing the size.
I use this in the following way:
UIButton * anotherButton = [self niceSizeButtonWithTag:1];
[anotherButton setImage:[UIImage imageNamed:#"image" withExtension:#"png"] forState:UIControlStateNormal];
[anotherButton setCenter:CGPointMake(middleOfView)];
[aView addSubview:anotherButton];
I create a lot of buttons like this, hence the reason for the method.
The buttons are always created and added to the subview perfectly. I can see the image and they're in the correct position. The problem is that they only respond to touches in a tiny strip.
In this attached image,
alt text http://web1.twitpic.com/img/107755085-5bffdcb66beb6674d01bb951094b0e55.4c017948-full.png
The yellow shows the whole frame of the button,
The red shows the area that will respond to touches.
The grey shows a section of the view the button is added to.
If anyone could shed some light on why this is happening, it would be really useful.
UPDATE:
I should probably mention I'm trying to add this button to a UIView which is a subview of a UITableViewCell.
UPDATE 2:
Adding it directly to the UITableViewCell works, adding it to the UIView on the UITableViewCell is causing the error.
FINAL UPDATE
The problem turned out to be that I was sending the view containing the button to the back of the subviews on the UITableViewCell. Getting rid of the sendSubviewToBack: call fixed it.
Do you have any other views added to this containing view after you add the button? try setting some of your view's background color to blueColor, redColor and other colors to better see what the stack of views in your app is like. Then you should be easily able to see if there is some sort of view blocking the button.

Not calling the function when I click on UIButton?

I have an imageView and after i add it to the screen I release it.
Before that I adds a button onto it with this code.
button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.frame = CGRectMake(128.00, 128.00, 23.00, 40.00);
[button setBackgroundImage:[UIImage imageNamed:#"move.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(showAddress:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button];
- (void) showAddress:(id)sender
{
NSLog(#"Working");
}
The button appears but nothing happens when I click on it to call a function. Not even the pressing-unpressing. Is this because I am releasing the imageView?
Edited:Check if userInteractionEnabled property of your UIImageView is set to YES - it is set to NO by default and that's why UIImageView (and its subviews) does not respond to touch events. I've just set it to YES in my code and it works fine.
One more note - when you add your button to another view it takes the ownership of the button and as you retained it in your code you should also release it. (Or just not retain in this case)