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

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.

Related

UIButton touchUpInside event not firing correctly

I have a tableview that contains a row with a custom cell that contains a UIButton. However, the button doesn't always fire the action. Here's my code:
submitButton = [[UIButton alloc] init];
[[submitButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[submitButton setClipsToBounds: YES];
submitButton.backgroundColor = [UIColor grayColor];
[submitButton setTitle:#"Send" forState:UIControlStateNormal];
[self.contentView addSubview:submitButton];
[submitButton addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[submitButton release];
This is called in the custom cell's -(id)initWithStyle:
The buttonAction method looks like this:
-(void)buttonAction
{
NSLog(#"Button Clicked!");
}
It seems that the only way I can get the buttonAction to fire is if I press down on the button and release somewhere inside the cell's frame, but not inside the button itself. Why would that be?
*UPDATE*
Problem still exists, but I found that the more consistent way to get the button to fire is to click and drag to the left or right and then let go, as long as I let go within the bounds of the cell/row.
UPDATE #2
It looks like if I use iOS 6.0, it works as intended. But on 5.0 or 5.1 it does not.
Try assigning the same method call to TouchUpOutside as well - you should then see it work every time. TouchUpInside is only fired if you lift your finger while still within the bounds of the button.
Try [submitButton sizeToFit]. I'm wondering whether your button has any size (since I don't see you giving it any).
Also: Create your button with [UIButton buttonWithType: UIButtonTypeCustom] instead of alloc-init.

iPhone SDK - UIButton Highlighted State Question

I am having trouble with the behavior of one of my UIButtons. I am trying to essentially make it a toggle button, but I am running into the problem below.
I have the code:
UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(horizontalOffset+buttonWidth, verticalOffset, buttonWidth, buttonHeight)];
[likeButton setImage:[UIImage imageNamed:#"like-off.png"] forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:#"like-on.png"] forState:UIControlStateHighlighted];
[likeButton setImage:[UIImage imageNamed:#"like-on.png"] forState:UIControlStateSelected];
[likeButton addTarget:self action:#selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
which fires the method:
-(void)likeButtonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
[button setSelected:!button.selected];
}
The behavior I am seeing is that when I tap the button down (and highlight it), it works as expected, and the 'like-on.png' image is used for the highlighted state, and it remains on in the 'selected' state.
However, when I tap the button again, to toggle it off, I see a gray highlighted state when I press my finger. When I release my finger, I see the 'like-off' image is shown as expected.
I would like to avoid seeing the gray highlighted state when I press my finger down on the button when I go to toggle it off. Instead I would like to make sure that the highlighted state on toggle-off uses the 'like-on.png' image as specified in the code.
What's going on here? Any ideas where my code could be incorrect?
Many thanks,
Brett
You're missing the image for the selected and highlighted state:
[likeButton setImage:[UIImage imageNamed:#"like-on.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
If you don't set it, the image of the normal state is used. From the -[UIButton setImage:forState:] documentation:
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value.
If you don't want your images to be modified when they are highlighted, set:
likeButton.adjustsImageWhenHighlighted = NO;
I think the selected property of UIButton is meant for something different (think of the desktop UI).
It would be more consistent to change the for all states according to a BOOL that tracks if it is "on" or "off".
Thus,
-(void)likeButtonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
liking = !liking;
if (liking) {
// configure the four states with "like-on" and other images
}
else {
// configure the four states with "like-off"
}
}
Otherwise you would use the state of a UI element to represent your program logic, which is basically flawed. The only instance where this is sort of acceptable (but not really) is a UISwitch.

Add subview to UIButton

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

UIButton oversensitive

I have a UIButton defined within a tableviewCellWithReuseIdentifier.
The button works but it's very touchy. If I just tap the button it works. Pressing it any long fails to trigger the action, even though it does flash showing that it knows it was pressed. Why is this happening? More importantly, how can I fix it.
Here is the code for the UIButton within the cell.
CGRect rect = CGRectMake(190.0, 2.0, 40.0, ROW_HEIGHT);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTag:LBUTTON_TAG];
[button setFrame:rect];
[button addTarget:self action:#selector(leftbutton:) forControlEvents:UIControlEventTouchUpInside];
[button setAlpha:0.5];
[cell addSubview:button];
A long shot, but: do you have any asynchronous background processes that might be calling [tableView reloadData] between tap-down and tap-up? That might cause UITableViewCell's mouse tap handling to reset some internal data that makes it "forget" the tap-down inside the button, which could cause it to not fire the UIControlEventTouchUpInside event since it doesn't remember the tap-down.
Possibly because your finger is moving slightly so UIScrollView (which UITableView is a subclass of) thinks it's a drag?
Try setting tableView.canCancelContentTouches = NO.

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...