I have a UICollectionView and I want some of my cells to be disabled. That means I want the cell to look a bit greyer (to have a disabled feeling), and I want the user to not be able to click on the cell.
I know I can use the alpha parameter and set it to something like 0.7, but this gets disabled while moving the cells around, and also it doesn't look grey enough. So I'm looking for another option to do it.
For controlling the tapping on a cell, I know I can use collectionView:didSelectItemAtIndexPath: and check for the indexPath. But I was wondering if there's another option for disabling a cell, like there is for disabling a UIButton: myButton.enabled = NO.
You can always set userInteractionEnabled to NO for the UICollectionViewCell and any taps inside it will be ignored.
Your problem with the alpha sound like you need to do some more work in prepareForReuse, or implement prepareForReuse in your custom cell class if you have not already done so.
Related
I'm setting images for a UIButton's normal and highlighted states. However, since it's in a UIScrollView, unless I set the scroll view's delaysContentTouches to false, the button's highlighted state doesn't show unless the user long-presses the button.
Delaying content touches isn't ideal, since the user can't initiate a scroll gesture on top of the button.
However, it seem like somehow UIButton.showsTouchWhenHighlighted is able to show the highlighted state immediately upon tap without the need to set delaysContentTouches to false. Is there a way to be using UIButton to get the same behavior for its images?
Since it seems like UIButton.showsTouchWhenHighlighted and UIControl.State.highlighted have differing criteria for what it means to be "highlighted", I ended up making a custom UIButton which overrides setImage to simply save the images in custom properties and overrides touchesBegan/touchesEnded/touchesCancelled to call super.setImage and actually set the image (only ever setting .normal). I'm just using .normal and completely ignoring .highlighted, since state doesn't behave as I'd like (how showsTouchWhenHighlighted behaves).
I have a few UIButtons inside UITableViewCells, and all of them are missing default tap animation, however if I long press - animation works. I've found some solutions, like setting type to .system, showsTouchOnHighlight = true, but none of them have helped. What is the problem here?
It's not a "problem" - it's by design.
When you have an object such as a button in a table view or scroll view or collection view, and you "touch" it, the system needs to know whether you are tapping the button or if you want to touch-and-drag to scroll the view that contains the button.
So, the table view (really, the "scroll view part" of the table view), waits to see if you drag your finger or not before it performs any actions.
If you want the button to respond immediately to a touch, you need to set
delaysContentTouches = false
on the containing scroll view(s).
If you search for uibutton inside uitableviewcell delaysContentTouches you should find plenty of discussion on this topic, and various approaches to change the default behavior.
For this problem you can add extension to UIButton and override some methods.
You can check my answer here
I’m implementing accessibility in a custom UITableViewCell class. I have a fairly simple overflow menu with a couple of buttons inside it, which are hidden until an ellipsis button is pushed that slides open and closes the overflow.
In my cell's initialiser I’m setting the accessibilityElementsHidden of my overflowContainer to YES. This seems to work, when scrolling through using VoiceOver, those views are skipped.
Then, when opening the cell, in the completion handler of the UIView animation, I set that same accessibilityElementsHidden of the same overflowContainer to NO. This doesn’t seem to have any effect, those elements are still skipped.
I’ve also tried posting UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil) before / after / when changing the accessibilityElementsHidden BOOL, but this also appears to have no effect on the situation.
Basically I’d like to toggle accessibilityElementsHidden on a couple of UIView instances at a specific point. Could anyone let me know what I may be doing wrong?
Here’s the code I fire when the overflow opens:
- (void)cellOverflowDidShow:(MyCell *)cell
{
self.overflowContainer.isAccessibilityElement = YES;
self.firstButton.isAccessibilityElement = YES;
self.secondButton.isAccessibilityElement = YES;
self.thirdButton.isAccessibilityElement = YES;
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.firstButton);
}
I fire the opposite when closing the cell (set all to NO and post notification again). And when initializing the cell, all I set is:
self.overflowContainer.isAccessibilityElement = NO;
Absolutely no idea why it shouldn’t be working, it appears I’m doing everything 100% correctly. If I don’t set the line in the initializer, the buttons all appear accessible (all the time). So it appears that the first call, be it YES or NO, works, but any subsequent ones are ignored.
In the visible state, you declare the overflow container to be an accessibility element. Thus, VoiceOver will allow the user to focus it rather than navigate child elements. Instead of toggling whether it's an accessibility element, keep self.overflowContainer.isAccessibilityElement set to NO and toggle the accessibility of its children, firstButton, secondButton, and thirdButton.
A shorthand for setting the accessibility of child elements is accessibilityElementsHidden. Try setting self.overflowContainer.accessibilityElementsHidden to NO when the view appears and YES when it disappears.
You may still need to trigger a layout change notification, regardless.
The Issue
I have a UIGestureRecognizer setup that on press to any of the UITableViewCell in my UITableView, it sets the UISearchBar active. Everything works. I can press on the cell and the UISearchBar animates as it would normally. I can enter letters, tap and hold on the UITextField and zoom in to a specific cursor position. I can hit cancel and everything goes back to their proper locations.
This is where the issue comes up. I scroll down a few cell (or until the UISearchBar is hidden under the UINavigationBar) and press the cell to activate the UISearchBar. Everything seems to animate to their proper location but when I try to tap and hold to zoom in to a proper cursor on the textfield nothing happens. I am also unable to hit the cancel button. The odd part is that if I do press on any part of the UISearchBar, it becomes the first responder.
Things I have done
I played around with the contentInset of the searchResultsTableView and see if that was blocking the UISearchBar.
I played around with the frame of the searchResultsTableView and its superview to see if it is blocking the UISearchBar.
I added the methods defined in the UISearchDisplayDelegate protocol and ensure that there were no views blocking said UISearchBar.
Please help!
My goal is to be able to interact with the UISearchBar as it is intended. If you know of any other option I can please let me know!
Thank you in advance!!
Update
I found the answer at Programmatically activating UISearchBar blocks user interactions with it
There is a possibility of a timing issue when the UISearchBar is shown and when it becomes active so the solution is to delay activating it.
So when you scroll down, the tableview is actually not referencing to the original cell as a result of the way iOS tries to reuse cells to conserve memory. The way to solve this is to say if(indexPath.row==0){cell.contentview addGesture...} in the cellForIndexPath function or use the didSelectAtIndexPath function to specify what happens when you click indexPath.row 0
Is this possible? It seems shoddy for it not to be.
I'm trying to create a toggle button (not a UISwitch) in IB. For example a mute button, when muted would have some indication that sound is disabled, when you hit it, that indicator disappears (but still the same underlying graphic), and toggles between these states each time it is pressed.
This functionality can be achieved using the selected property, however you can't change the Selected AND Highlighted property in IB like you can in code, so whenever the button is pressed, no matter the state, the highlighted image is the same, which looks terrible and glitchy.
Is there a way to fix this with just IB, or do I have to make a custom class to avoid manually loading in all these buttons?
Have you thought about subclassing UIButton to add in the things you need?