Custom UIButton inside a UITableView keeps highlighted after being pressed - ios

I have implemented a custom UIButton, posted in cocoacontrols.com:
http://cocoacontrols.com/platforms/ios/controls/imageless-gradient-buttons
I´ve just converted the project to ARC and setted up the buttons. They work, but after being pressed once, they keep they highlighted gradient.
To set them up, I just changed the class on both storyboard and on the IBOutlets and I´ve selected the gradient I wante on viewDidLoad.
Any idea on what could be the problem?
EDIT: I've been spending more time with it and it appears to be that the problem only happens when the button is on a UITableView. It works OK when it is on a UIViewController
EDIT #2: according to the NSLogs, when the container is a UIViewController, the gradient is back to normal when a button is pressed:
2012-05-26 10:53:17.950 GradientButtons[11507:f803] highlighthed
2012-05-26 10:53:18.040 GradientButtons[11507:f803] highlighthed
2012-05-26 10:53:18.140 GradientButtons[11507:f803] Normal
but when the container is a UTableViewController, the button remains with the highligthed gradient:
2012-05-26 10:55:20.969 GradientButtons[11507:f803] highlighthed
2012-05-26 10:55:21.069 GradientButtons[11507:f803] highlighthed
I've checked the viewContentMode and it is all the same for all the buttons, UIView and UITableView (Scale to Fill). Changing it to redraw doesn't change behaviour.

There's the check responsible for normal/highlighted gradient drawing:
if (self.state == UIControlStateHighlighted)
gradient = self.highlightGradient;
else
gradient = self.normalGradient;
You need to set the breakpoint or add NSLog's to see whether the normal gradient is selected there. If it doesn't, the next step would be to override the normal UIButton drawRect adding the state log. Use it instead of the colored buttons to see whether that's a standard UIButton behavior for your table (e.g. you might keep the cell highlighted after the button is clicked which forces the subviews also to be highlighted)
Based on your log i found it interesting an reproduced the problem, it seems to be a race condition for the highlighted state to be caught at touchesEnded (try to hold a click for about a second - the state will be normal at touchesEnded). The reason needs to be investigated further, as a workaround you could use the following code:
-(void) setHighlighted:(BOOL)highlighted
{
NSLog(#"setHighlighted %#", highlighted ? #"Y": #"N");
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}

Related

iOS 7 custom keyboard UIView touch down event delayed in bottom row

here's an odd one..
I've got a UIView xib file that looks like this:
I've connected every UIButton touchDown and touchUpInside events to two IBAction methods:
- (IBAction)touchUpInside:(id)sender
{
NSLog(#"touch up inside");
if (((UIButton *)sender == _enter) | ((UIButton *)sender == _back)) {
[(UIButton *)sender setBackgroundColor:_color2];
}
else {
[(UIButton *)sender setBackgroundColor:_color1];
}
}
- (IBAction)touchDown:(id)sender
{
NSLog(#"touch down");
[(UIButton *)sender setBackgroundColor:_color2];
}
Everything works except for the bottom-most row of UIButton's, that's the odd part:
The touch down event is fired, but the button must be held for 0.5 second for it to change background color, whereas it is instantaneous for the other buttons.
It ONLY happens for the bottom-most row of UIButton's, as I've tried to switch buttons 7, 8, 9 with buttons #back, 0, #enter like this:
I've checked in Interface Builder all the UIButton attributes are the same, and I've tried moving the UIButton's objects order around as you can see on the left side of the picture, and I'm about out of ideas already. Basically what's odd is the UIControl behavior differs based on its position on the parent view...
UPDATE: I made the parent UIView height value large enough that there is 50 free pixels below the last row and the UIButton's work fine now. The only reason I can think of now is that there is a UITabBar there 2 view controllers level underneath. Even so it doesn't make sense.
The document says:
Expect users to swipe up from the bottom of the screen to reveal
Control Center. If iOS determines that a touch that begins at the
bottom of the screen should reveal Control Center, it doesn’t deliver
the gesture to the currently running app. If iOS determines that the
touch should not reveal Control Center, the touch may be slightly
delayed before it reaches the app.
One solution is here:
UIButton fails to properly register touch in bottom region of iPhone screen
But, in your case, I think you should use inputView in UIResponder.
See: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html
The inputView is not affected by that problem.

Why a UIButton on UITableViewCell only drawn darker when touch gesture continued for a short time

Pressing the button quickly and not holding for a short time, will not highlight the button.
Different from a UIButton on a common UIView.
Like the head photo in official Twitter client got same issue.
Instagram client seems solved this, all buttons works fine.
Find same question here:
Why doesn't UIButton showsTouchWhenHighlighted work when the button is on a UITableViewCell?
But I still don't know how to fix it.
Well... a UITableView is a subclass of UIScrollView and the UIScrollView class is known to eat touches for it's own purpose.
When it realizes the touch was not meant for it, it passes it to it's immediate subview.
This feature is the delaysContentTouches property (which by default is YES).
Which is why, the UIButton shows it's highlighted state only after a extended touch because the touch event was with the UITableView for a short while until it determined whether the touch was meant for scrolling or swiping the cell and on realizing the touch was for neither, it immediately passes the touch event to the subView directly below it.
In case of a quick-tap, the button's highlighted state is bypassed due to this delay and the target selector method is called directly.
To show the highlighted state of the button in a UITableView (just as it would on a UIView) do:
For iOS7+:
In -viewDidLoad or anywhere appropriate do:
[yourTableViewObject setDelaysContentTouches:NO];
Also... The cell.subviews has a class UITableViewCellScrollView which apparently is another scrollView and we need to disable the delaysContentTouches property of this class as well.
So... in the -cellForRowAtIndexPath: method (just before return cell;) do:
NSArray *test = cell.subviews;
for (UIView *currentView in cell.subviews) {
if ([NSStringFromClass([currentView class]) isEqualToString:#"UITableViewCellScrollView"]) {
UIScrollView *svTemp = (UIScrollView *) currentView;
[svTemp setDelaysContentTouches:NO];
break;
}
}
For iOS 6-:
In iOS6, the cell.subviews has a UITableViewCellContentView class which is not a scrollView subclass and so all it takes is setting one parameter for the tableView alone.
So, in -viewDidLoad or anywhere appropriate, this is all that you need:
[yourTableViewObject setDelaysContentTouches:NO];
PS: By doing this, it will mess up with the scrolling of the tableView so use your better judgement.

UITableView delete button overlaps to content

I'm preparing a UITableView with a custom prototype cell having a UISwitch widget on the right side, and I'd like to let my users be able to delete rows.
Everything is fine with that, except the fact that when the delete button shows up it overlaps to the UISwitch, this way:
Is it possible to have the UISwitch shifting left when the delete button appears?
Epilogue
I've decided for brevity to not shift my UISwitch position when "delete" button appears, but to make it disappear, bringing it back when the "delete" button is gone.
So, according to #geo suggestion (thank you), I've managed it out (quite simply) this way:
In my UITableViewCell' subclass .m file:
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
activationSwitch.hidden = YES;
}
else {
activationSwitch.hidden = NO;
}
}
I hit a very similar problem and fixed it. You need the right autoresizing mask (assuming not doing auto layout here) for your UI elements in your custom tableview cell in Interface Builder.
In my case, I needed to add the Left constraint (see that little autoresizing picture/animation in the Size Inspector, View section) for each of my UI elements.
Add a Left "bar", and you should be good.
Override the Methode
-(void)willTransitionToState:(UITableViewCellStateMask)state
-> UITableViewCellStateShowingDeleteConfirmationMask
of your custom Cell and do there your customizing :)

Custom UINavigationBar animation

I've subclassed UINavigationBar in order to set a custom background image.
I'm using it to initialize a UINavigationController with:
-(id)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
The problem is that when new UIViewControllers get pushed on to the NavigationController stack, the title text animates in from the right across the background image in a way that looks a bit sucky.
Problem: How to tweak the animation so e.g. it fades in as it animates across.
The closest I've come is by creating a custom fade-up animation in the pushed/popped ViewController class. I use a NSTimer to trigger changes to the alpha of the titleView:
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
...
-(void)setAlpha:(float)alpha
{
titleView.textColor = [UIColor colorWithRed:grayShade green:grayShade blue:grayShade alpha:alpha];
}
However, if I trigger this in 'ViewDidAppear' it's already too late and the fade-up starts after the new view has finished sliding in. I figured that I could trigger it in 'ViewWillAppear' but that this might lead to indeterminate and inconsistent timings on different devices. Am I right in this assumption and if so, how can I customize the animation in the way I want to?

How to set the UIButton state to be highlighted after pressing it

I have a typical requirement wherein I need to keep a button in highlighted state after pressing it. I need to perform a task which should work only when a button is in highlighted state. Actually I am setting a button state to highlighted programatically.
[sender setHighlighted:YES];
And once the button is in highlighted state i need to perform another action.
- (IBAction)changeState: (UIButton*)sender
{
if (sender.highlighted == YES)
{
[self performSomeAtion:sender];
}
}
But, to my horror, whenever I press any button, the above condition is becoming true and the action is being performed repeatedly. Is there any way in which i can keep a UIButton's state to be highlighted after pressing it?
EDIT - Actually I need to perform 3 different actions for 3 different states of the button. I am already making use of selected state and normal state. Now, I need to make use of the highlighted state.
[sender setSelected:YES];
or you can simulate this effect with two image for your UIButton (notselectedimage.png and selectedimage.png), then keep track button state with a BOOL variable like BOOL buttonCurrentStatus;. Then in .h file:
BOOL buttonCurrentStatus;
and in .m file
// connect this method with Touchupinside function
- (IBAction)changeState:(UIButton*)sender
{
/* if we have multiple buttons, then we can
differentiate them by tag value of button.*/
// But note that you have to set the tag value before use this method.
if([sender tag] == yourButtontag){
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
[butt setImage: [UIImage imageNamed:#"selectedImage.png"] forState:UIControlStateNormal];
//[self performSomeAction:sender];
}
else
{
buttonCurrentStatus = NO;
[butt setImage:[UIImage imageNamed:#"notSelectedImage.png"] forState:UIControlStateNormal];
//[self performSomeAction:sender];
}
}
}
- (void)mybutton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = ![button isSelected]; // Important line
if (button.selected)
{
NSLog(#"Selected");
NSLog(#"%i",button.tag);
}
else
{
NSLog(#"Un Selected");
NSLog(#"%i",button.tag);
}
}
The highlighted state is used to highlight the button while it is being touched. A touch down event in the button highlights it. You should use the "selected" state instead.
If what you want to do is perform an action after the button is pressed, don't attach your method to the state change event, attach your method to the TouchUpInside event.
I just find a way, so I share it, just in case...
I kept my UIButton and set one image for each state (so you could go up to a 4 states button).
I set the UserInteractionEnabled to NO -> This button won't receive any touch.
The purpose of this first button is to show a state
I create a second custom UIButton with the same frame than the first one. For this one, none image will be set for the state (it's a fully transparent button). The purpose of this button is to catch the touch event. So I added a target to this button on the TouchUpInside event. And then when the event is fired, I change the state of the first button to Disabled, Highlighted, Selected, or none of these state (= Default state).
Everything is working like a charm!
The way you describe it, you'd be better off subclassing UIView to create your own three-state button.
Actually, you should even implement your own multistate buttonView, and manage the state it's in internally via an array of PNG for the looks and an array of states to know how many times it's been pressed.
Use [sender setSelected: YES];, I think it will be useful to you.
UIButton *btn_tmp=sender;
if(!(btn_tmp.selected))
{
[btn_temp setHighlighted:YES];
}
For iOS 7 only: you should consider setting the image renderMode to UIImageRenderingModeAlwaysTemplate. You can then use the tintColor to represent various states.
see How to apply a tintColor to a UIImage?
and
see Tint a UIView with all its subviews
The solution is tricky but it's possible.
The problem is that you tried to change the highlighted status in the button action method, which I suppose makes a clean up or check process at the end of the action and switch the highlighted status. When you try to debug it you get the highlighted = 1 but it will change at the end.
Strange but your "3 statuses button" is sometimes useful, when you'd like to keep a button in "highlighted" mode like the "selected" mode to get different action depending on the 3 statuses.
The only problem that you couldn't analyze this or switch it to highlighted mode in the button action method as this will switch to highlighted mode immediately as the user push it AND switch it back at the end.
The solution is using a dispatch.
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[theButton setHighlighted:YES];
});
This will do the trick and you could use the 3 statuses.
According to apple, UIButton has a property of imageView:
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view
This means that you can set in the IB (in the storyboard) a picture for this button and set the highlighted picture:
Open the Attribute inspector.
Under Button section, choose an image.
In the same section, change the State Config to Highlighted. Notice the image you chose under default is now gone and now you can set a new picture for the Highlighted.
Now you have a button with 2 state config and all you have to do during runtime to change the button.highlighted = true. Also, check the UIControl under Configuring the Control’s Attributes for more states.
You can also do it programatically as follows:
Swift (and almost the same in Objective-C):
// Setting the highlighted image
self.someButton.imageView?.highlightedImage = UIImage(named: "imageNameFromImageAssest")
// someButton will now some the highlighted image and NOT the image set in the IB
self.someButton.imageView?.highlighted = true

Resources