So here is my setup:
UICollectionView configured with UICollectionLayoutListConfiguration
appearance is UICollectionLayoutListAppearancePlain
headerMode is UICollectionLayoutListHeaderModeSupplementary
Using UICollectionViewListCell with content configuration
Implementing collectionView:viewForSupplementaryElementOfKind:atIndexPath: to return custom section header
Implementing indexTitlesForCollectionView: to return index titles representing the sections (e.g. "A", "B", "C")
Implementing collectionView:indexPathForIndexTitle:atIndex: to return the index path of the first cell in given section.
Now, when user taps on any index title, the collection view scrolls and positions the first cell of that section at the top. However, the header section (which is sticky) is now overlapping part of the cell.
Any suggestion on how to fix this?
I guess that a new delegate method, such as collectionView:supplementaryElementIndexPathForIndexTitle:atIndex: would probably target this use case (as the collection view would scroll to a section header rather than to a cell).
Related
I have a setup a collection view with remembersLastFocusedIndexPath set to true.
CollectionView is designed such a way that it has nested collection views,
CollectionView
-> Section 1
--->Header
--->Cell nested with collectionview
-> Section 2
--->Header
--->Cell nested with collectionview
....so on....
There is pagination too.
Now if user scrolls to say 2nd item in 7th section and clicks menu in tv remote, I am scrolling collection to top. [either by setting content offset to 0 or scrolling to 0th index]
Requirement: After scrolling to 0th index, if user tries to focus on first cell in 0th section, I want focus to be at 0th index.
Issue: But now collection view remembers last focused index path and scrolls to 2nd item in 7th section.
Expected: I do not want collection view to remember the last focused index path in this case. In all other cases, it should be remembered. i.e, after scrolling to top, I want do something like
collectionView.scrollToTop()
resetFocus() //which should reset collection view's focus to initial index.
func resetFocus() {
//What should be written here to reset the focus of collection view.
}
try also implementing indexPathForPreferredFocusedView after you reset focus. Documentation here.
I'm adding Accessibility support to my iOS app and I'm having trouble with a collection view in one of my table view cells.
For example, when the user scrolls (horizontally) from the first cell to the second cell, Accessibility still reads the contents of the first cell. If I try to tap on a view in the second cell, it highlights an empty space to the left of the second cell (where the first cell would be but no longer visible) and reads the contents of the first cell.
When the collection view is not in a table view cell (i.e. a subview of a UIView), this does not happen.
I'm suspecting this has something to do with calling UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification) and I've tried calling it in many different places, but nothing has worked.
The following two screenshots show a collection view inside a UIView. Accessibility is enabled, so it gets selected with a black border when tapped.
When user taps first cell, it will get selected.
When the user taps 'Next', goes to the second cell, and taps the cell, the new cell will get selected.
The next two screenshots show the collection view inside a table view cell.
When the user taps the first cell, it gets selected and VoiceOver properly reads "I'm label 0".
However, when the user taps 'Next', goes to the next cell, and taps the second cell, it does not get selected and VoiceOver will still read, "I'm label 0".
The code is available here on github.
I had simillar problem which I eventually resolved.
I think that you have mismatched elements with
isAccessibilityElement = true
over each other.
I have a table view which scrolls vertically and each cell contains a title and collection view which scrolls horizontally.
I set isAccessibilityElement to true only on title and collection view cells, false on the rest.
Then, I subclassed UICollectionView and overrode the following NSObject methods:
func accessibilityElementCount() -> Int
func accessibilityElement(at: Int) -> Any?
func index(ofAccessibilityElement element: Any) -> Int
It basically just tells the voice over that your collection view has these accessible elements. The collection view itself is not accessible which is not a problem, the contrary. You could probably use
open var accessibilityElements: [Any]?
instead.
Here is some more reading from documentation (UIAccessibility.h):
UIAccessibilityContainer methods can be overridden to vend individual elements that are managed by a single UIView.
For example, a single UIView might draw several items that (to an end user) have separate meaning and functionality. It is important to vend each item as an individual accessibility element.
Sub-elements of a container that are not represented by concrete UIView
instances (perhaps painted text or icons) can be represented using instances of UIAccessibilityElement class (see UIAccessibilityElement.h).
Accessibility containers MUST return NO to -isAccessibilityElement.
To allow nice 3-finger voice over scroll you probably want to override
func accessibilityScroll(_ direction: UIAccessibilityScrollDirection) -> Bool
as well and scroll your collection view accordingly.
You may want to try this:
let nextCell = collectionView.cellForItemAtIndexPath(nextIndexPath)
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification,
argument: nextCell)
in your onNextButtonTapped method, after your call to scrollToItemAtIndexPath.
This will focus Accessibility on the next collection view cell.
I have a tableview with a list. I want to add a cell to the top of the list that shows what is being displayed below. So each item in the list is a Name with Age and Gender next to it. I want a header cell on top that actually says "Name Age Gender" so that people actually know what data is being displayed. I also want this cell to not scroll when the rest of the list scrolls downwards.
Do I add another cell to show these things or should I do it via another method?
I think you're talking about a header of a table view section. If so, you can implement the header cell methods in UITableViewDataSource:
-tableView:titleForHeaderInSection:
Alternatively, if you want to get fancy and use your own view for a section header try implementing the UITableViewDelegate method:
-tableView:viewForHeaderInSection:
I have a UITableView with expand and collapse cells when I tap in the sections. Now I want to collapse the cells again when I tap in one of this cells and give the name of this cell to the section up there. I think that collapse is not a problem but I do not know how to change the name. I have done everything programmatically. Thank you
You can try like this:
add an observer in custom section view to update title. Because section view is reused in UITableView,need a parameter(such as: int sectionIndex) to definite witch section the section view belong to.
in tableView:viewForHeaderInSection: set sectionView.sectionIndex with indexPath.section
in tableView:didSelectRowAtIndexPath: post a notification with indexPath.section and title string
UITableView returns the same callback "didSelectRowAtIndexPath" with the same NSIndexPath (0,0) both for section click and section first row click. I'm using custom view for section header view and I need to perform some action on these section rows. Tried checking cell class with [tableView cellForRowAtIndexPath:indexPath] but it's obviously returning the same row cell instead of section cell. Any suggestions?
UPDATE I could add my custom section view to first row instead of adding it as a section, however in that case, I would need to return different row height in "heightForRowAtIndexPath" and that would be not-performanc-wise decision.
UPDATE I'v designed my section view as a subclass of UITableViewCell, because I prefer to get native UITableView callbacks instead of workaround'ing with tap gestures or buttons.
Centurion, if you want to have section that you open/close easily, I suggest you to use the class APLSectionHeaderView.
You can find more information on APLSectionHeaderView.h and APLSectionHeaderView.m
Hope it will help you.
I've been using it, so if you have some question about it...