I have a UICollectionView with a custom UICollectionViewCell. The UICollectionView scrolls horizontally, with each cell occupying all of the visible screen.
The UICollectionViewCell has a UIView subview, which in turn has a UIScrollView subview.
The UIScrollView is intended to scroll vertically, but it's not scrolling at all. I've tried setting the scroll view's contentSize with no success.
I suspect that the UIScrollView is not getting any touch events rather than it being a size issue.
Any suggestions?
EDIT >>
I'm now sure it's an event problem rather than anything specific to the UIScrollView.
I've now overridden the pointInside: method in the UIView in the UICollectionViewCell and can see that it now returns false every time I tap on it. In that case you'd think that the tap event would propagate to the next subview , but the UIView still isn't getting events. I've tried adding a UIGestureRecognizer to the UIView but it never registers a tap.
Could there be anything here intercepting the events that I'm not aware of?
I've been trying to solve a similar problem with a scrollview, and your edit about events reminded me of this question (which solved my problem again) How can I use a UIButton in a UICollection supplementary view?
Its possible you need to be using a UICollectionReusableView, not a UICollectionViewCell. Changing the class worked for me because I was using a button (and recently a scrollview) in a header.
However I've not tried for cells themselves. For capturing events from a UICollectionViewCell, maybe the following may help?
Getting button action : UICollectionView Cell
Try to disable userInteractionEnabled for the UIView and enable it for your UIScrollView
I'm not sure if this was your problem, but it was mine - and I'm putting it here in case anyone else comes across this for the same reason. I wanted to put a UIScrollView inside a UICollectionReusableView but by accident I had created my custom class as;
class CustomCellHeader: UICollectionViewCell {
}
instead of;
class CustomCellHeader: UICollectionReusableView {
}
Once I changed that, the UIScrollView within my header cell came to life!
Embed the scrollable content in a ScrollView within the cell.
Add "UICollectionViewDelegateFlowLayout" to the UICollectionViewCell i.e.:
class SomeCollectionCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout {
}
Now scrolling within the cell should work.
Related
Recently, I see a strange thing in Xcode 11.4.
When I create a UICollectionView with Its cell, adding TapGesture doesn't work. By debugging on its view in runtime, I noticed a view covers all the cells. It seems it is ContainerView.
This view prevents users to click or tap on items.
Any help is appreciated
Are you adding your subviews and tap gesture to the cell's contentView?
In the documentation for UICollectionViewCell (https://developer.apple.com/documentation/uikit/uicollectionviewcell), it says:
To configure the appearance of your cell, add the views needed to
present the data item’s content as subviews to the view in the
contentView property. Do not directly add subviews to the cell itself.
The same applies for UITableViewCell as well.
I want to put my UIView inside of UICollectionView(on top) that on scroll it scrolls with UICollectionView content. It's possible to put it on header part in UITableView, I did it, but in UICollectionView I cannot achieve the same effect. How can I do it?
I have created GitHub repo with sample. If anyone could help me with it.
In UITableView I add UIView inside of UITableView and it stays there, but in UICollectionView it stays behind of UICollectionView. I want that it(UICollectionView sections) will stay under the UIView and on scroll all(UIView & UICollectionView sections) will scroll together.
There is no such thing as collection view footer or header, because the layout can be very different depending on your needs, but, you have a couple of function that can help you for example:
registerClass(_:forSupplementaryViewOfKind:withReuseIdentifier:)
or
registerNib(_:forSupplementaryViewOfKind:withReuseIdentifier:)
depending if you are using a nib file or not.
I'm not sure but you will also need to provide the height of the view in the layout delegate
UICollectionViewDelegateFlowLayout
you can do this by dragging your UIView on to your cellidentifier of UICOllectionView
Like i did see in snapshot:
here View is my UIView which i added to UICollectionView and it scroll with the CollectionView
I have a UITableView in my view controller, and inside the cell there's a horizontal UIScrollView and inside this scroll view, I created many instances of a custom UIView (loaded from NIB file)
UITableViewCell
-- UIScrollView
-- MyView: UIView
Now MyView is not detecting touches, userInteractionEnabled is set to YES on every view in the hierarchy and I tried both ways
Implement touchesEnded: in MyView
Add a UITapGestureRecognizer to it
Both ways don't work, I guess it's something related to the fact that I have a UIScrollView inside the UITableViewCell
I am writing the application in Swift, not Objective-C, I don't think it matters since I guess it's a UIKit issue but who knows.
If you have any hint, I am all ears.
Thank you
Alternative for this case is add UITableViewCustomCell and add UICollectionView with horizontal scroll in Custom cell . It is more optimized way than adding UIView in scroll view because you can greatly speed things up. Instead of instantiating a lot of cells, you just instantiate as many as needed, i.e. as many that are visible (this is handled automatically). If scrolling to an area in the list where there are "cells" that haven't got their visual representation yet, instead of instantiating new ones, you reuse already existing ones.
I have a UIScrollView inside a UICollectionViewCell so I can zoom an image within that ScrollView.
When I click the image I would like the CollectionViewCell to receive the event and pass it to the UICollectionView delegate.
I tried adding the following UIGestureRecognizerDelegate to the container of the UIScrollView:
-(BOOL)gestureRecognizer:ShouldReceiveTouch:
According to the answer I read the touch event should be passed on to the parent view - in this case the CollectionViewCell, but it isn't happening.
What should I do?
Edit
Please note that I'm not trying to pinch zoom inside the cell but rather be able to receive the touch events of the cell.
Thats a good question as I was having the same problem. The only way I could get the uicollectionviewcell to respond to touches (intercepted from the uiscrollview) was to set
userinteractionenabled = NO
on the scrollview.
When I reorder my UITableViewCells, the Subviews of the Cell are not visible while dragging...I get always the same result, whether I add the Subviews programmatically in a UITableViewCell Subclass or in Storyboard...
Is there a possibility to see the real UITableViewCell with Subviews while dragging?
Ok I found a solution by my self...
For some reason the mysterious reordering animation from Apple cant capture simple UIViews, but with UIImageViews, UIButtons and UILabels as Subviews of the custom TableViewCell, the animation works like expected...creepy!