Adding subview to UICollectionView - disables scrolling - ios

I want to add a subview to a UICollectionView in order to make a left panel that scrolls with the collectionview.
Using
[self.collectionView addSubview:myView]
all touches become disabled and I can no longer scroll the view. I've read that adding a subview to a collectionView like this is bad practice.. is this true? Why does it disable touches from reaching the collectionView event when
userInteractionEnabled = NO
I'm trying to do this: imgur link by grabbing the frame position of the first cell in each section, and adding a dot with to myView with the same y value.
Thanks for any help!

Adding subviews using the addSubview: method to a UICollectionView is very bad practice. It can cause a lot of different problems in the normal behaviour of the CollectionView. It can obstruct the views underneath it, capture the touch events, preventing them from reaching the actual scrollView inside the CollectionView, etc. The subviews added using this method will also not scroll as the other elements in the CollectionView do.
The correct way to do what you want is to implement a new type of UICollectionViewCell for the dots, and compute their locations in the prepareForLayout and layoutAttributesForElementsInRect: methods. Basically you'll have either one or two cells in each row. Which ones will have two rows will be determined by you in the methods I've mentioned.
In fact, Apple's docs have a perfect example that's even more complex than what you're trying you achieve. You should check it out from this link.

May I know the purpose of that scroll view ? Because, if you're looking for a subview that displays only a label or image etc., You can use custom collectionview cell instead if I am not wrong... Hope it helps :) :)

Related

CollectionView Cell inside UITableView is not scrolling horizontally for Voice Over accessibility

In my app, I have a screen where I need to support voice over accessibility. The screen has a TableView and inside that a collectionView. When voice over is ON and when I try to single or three fingers swipe, the collectionView scroll is not working. The accessibility focus is not moving to the next cell. Voice over is working fine only for visible cells in collectionView.
I tried by disabling accessibility for collectionView Cell and UITableViewCell but still, it didn't work.
I want to make it work like in the AppStore, where we will have a similar kind of design.
I am attaching screenshots of my screen and some codes.
On your country cell on awakeFromNib function or your init function depends how you initialize the cell add the line self.contentView.isUserInteractionEnabled = false after super call.
The purpose of your screen isn't really clear to me but I can suggest an interesting way to use collection views with VoiceOver:
Define a UIAccessibilityElement to be inserted as a carousel element (element of your collection view).
Set its trait to .adjustable in order to select the different elements by swiping right or left.
Adapt the label and the value according to the selected cell.
Define the collection view [accessibilityElements].
It may be tedious 😓 but that's what Apple's engineers recommend ⟹ a complete and detailed example is introduced in a WWDC session, don't hesitate to take a look at it to perfectly implement your collection view for the VoiceOver users. 👍
what you need is just to say to tableview cell that your collection is accessible doing something like this:
accessibilityElements = [collectionView]

Selectable UITableViewCells inside of a UIScrollView

I've been trying to programmatically create a UITableView inside of a UIScrollView and I'm having some trouble. Basically I want to have the cells to register as "checked" when they are tapped on. I don't need the UITableView itself to scroll because the UIScrollView will be large enough that this isn't necessary, but I still need the UIScrollView to be able to scroll when the user drags over the UITableView. I'm just overall confused about how gesture recognition works with regard to this project. I can't get the didSelectRowAtIndexPath method to be called, although I can get the didHighlightRowAt method to work. I saw a suggestion to add a UIScrollView onto each cell, but this didn't seem to help. Any guidance is appreciated as I'm unsure how exactly to go about this.
Thanks!
Sounds to me like you should be using custom UIButtons rather than UITableViewCells to allow for selectable content within your UIScrollView.

UITableView do not respond some touches while scrolling

When tableView is scrolling with high speed, and user tries to stop it by touch, tableView keeps scrolling. And user can stop scrolling tableView only after two or three touches. If speed of tableView scrolling is low, tableView respond touches well.
The tableView displays cells with a lot of subviews, and I localized problem on one view.
If I set problemView.userInteractionEnabled = NO, scrolling works well. To set userInteractionEnabled = NO for all subviews of problemView but not for problemView, do not help.
Here image of diplaying cell, the problemView is view with red frame.
Could somebody to give an advice, how to solve the problem?
There are several possibilities:
Your custom view, or one of its superviews, has userInteractionEnabled set to NO.
There is another view on top of your custom view, or on top of the table view within the custom view.
The custom view that the table view is inside is smaller than the table view (with clipsToBounds=NO, a subview that extends beyond the bounds of its parent can be seen but not normally interacted with). Or the same for another view in the stack.
The custom view (or one of its superviews) overrides pointInside:withEvent: or hitTest:withEvent: incorrectly.
It might be that it thinks you are editing it in some way. Try allowsSelectionDuringEditing = YES. Here is more information with using the UITableView.
If you are using NSTimer inside the UITableViewCell then I have experienced anomalous actions as you have discussed. In such case you need to replace implicitly scheduled timer with explicitly scheduled timer.
For me the problem was that having userInteractionEnabled = true with a click handler on subviews in the cell broke the scrolling. Not sure why this is the case though. I solved this in my case by using didSelectRowAt on the tableview instead of having custom click handlers on the subviews. Then both the scrolling and the click handling worked as I wanted to.

Two UIViews & one UICollectionView inside UIScrollView (or better approach)

I need to have iOS app with screen like this:
screen
The idea is when user start to scroll down the first UIView to move up until the second UIView reach the top where it will stick and only UICollectionView will continue to move up.
Currently I'm using this structure
UIScrollView (main scroll)
UIView (some banners)
UIView (UISegmentedControl)
UICollectionView (grid with items, scroll is disabled, main scroll is used)
I manage to do it, but I needed to set UICollectionView height constraint manually in the code (calculated based on all items in grid) in order to appear in UIScrollView (everything else is handled by AutoLayout in Storyboard). The problem with this is that UICollectionView think all cells are visible and load them, so the whole recycling & reusing thing does not work. It's even worst because I use willDisplayCell method on UICollectionView to load more data when last cell is displayed, but now it load all pages at once.
My question (actually they are 2)
How can I fix the issue above?
What is the right way to achieve this functionality? Maybe my whole approach is conceptually wrong?
Collection view is a scroll view itself. So maybe you could have same functionality only with Collection view with sections or even custom layout?

How to add UIScroller in UITableViewCell?

I have a table with different cells and I need to add a scroller view with special UI in some cells:
I need to develop the UI which you can see in section 0.
So my questions are:
How can I add a scroller view?
How can I load data only for those items which are shown right now?
How can I change the width of "Line" column after scrolling to the right?
I'll answer as best I can with some starting pointers for you...
You need to subclass UITableViewCell and in your subclasses drawRect method you should add a UIScrollView to the contentView.
Not totally sure what you mean here, please expand.
In your new table cell, set it as the delegate of the scroll view you implemented, then you''ll get callbacks when the scrollview scroll with the contentOffset value, which you can use to calculate how much to grow or shrink the line items.

Resources