I have the following structure:
- Scroll view
- UIView
- Collection view
I gave the UIView the following constraints:
top, left, right, and bottom = 0
Equal width and height to the Scroll view
I've also disabled scrolling for the Collection view, as I want the whole page to scroll together.
However, when items are added (dynamically) to the collection view and I try to scroll, nothing happens.
What am I doing wrong? How can I make sure that the page scrolls all together?
Maybe you should try removing the equal heights between view and scroll view because this limits view's height and doesn't allow collection view to grow.
Related
Let's say I have a scroll view that contains a number of subviews. One of these subviews is a table view, and since I already have a scrolling scroll view, I don't want my table view to scroll. I set isScrollEnabled = false, and I set my table view's top constraint to some other view in the scroll view, and its bottom constraint to the scroll view's bottom anchor. However, this setup causes the table view to be displayed with a height of 0.
Normally I would just set the height of the table view to be the height of each cell * number of cells. However, the height of each cell is also variable, so this will not be possible.
I have a tableView and a collectionView inside scrollView. Both of them have constraints for left, right, top and bottom. But both views are now visible when inside scrollView
Why may it be ?
I think scrollViews Need to know width and height for its subViews
Follow these steps:
Disable scroll for table view and collection view.
Resize the height of table view and collection view as per its content (If Dynamic height)
I have table view inside scroll view which will scroll according to table view cell width.I have set the scroll view from top,bottom,leading and trailing.Then I have given the same constraints to view inside the scroll view including centerX and centerY.Then for horizontal scrolling I have set the trailing and centerX priority to 250.But my table view is not scrolling.I want to set it through autolayout.
I want the table to scroll horizontally if label increases inside the table view cell.
This is my view hierarchy
#Zahid I made a public example on github with what I wrote about below:
https://github.com/amichnia/stack-54735700
Original answer:
As it was already said, table views do not scroll horizontally in general. But no one said you can't put table view inside UIScrollView that scrolls horizontally. There would be some challenges here though:
I don't think autolayout with autoresizing cells would work good.
You need to figure out scrollview/tableview gesture recognizers order. You can use UIGestureRecognizer delegate methods to resolve their collisions
You need to determine width of the table view. You can do it with old nasty way:
compute size of every cell first,
take max of these sizes
set it to table view width
reload data
Is it worth the hassle? That's your call to take.
With your setup (I see scroll view as root view) the simplest (but not best) option would be to disable scrolling on table view, enable scrolling both directions on scroll view, and just update whole table view size. It will work well only with small amount of cells though!
Been following tutorials online to create a scrollable list view in the storyboard.
I have done the following
View Controller --> Scroll View --> Content View
The scroll view is constrained to the View controller.
The content view width is constrained to the View controller.
The contentSize is set to 1000 height for the content view.
The story board is set to 1000 height.
The scroll view is set to 1000 height.
It appears to only scroll down half way
Any ideas on what i have done wrong here
Thank you
try to remove the height constraint for the scrollview, and fix it to the bottom of your parent view.
My view heirarchy looks something like this:
- View
- Scroll View
- Collection View
I'm using Auto Layout. The problem is that when I specify that the collection view should take the full width of the superview, it ends up actually taking the full width of the elements contained within it. I.e., if there were 1000 elements in this collection view and each element was 10 pixels wide, the collection view would be 10000 pixels. It appears to ignore my constraints.
The VFL I'm using looks a little like this:
Scroll View
H:|[scroll]|
V:|[scroll]|
Collection View
H:|[collection]|
V:|[collection]|
All views are set to not translate autoresizing masks to constraints.
when I specify that the collection view should take the full width of
the superview, it ends up actually taking the full width of the
elements contained within it.
The whole point of a scroll view is that it permits the display of views that are larger than the scroll view itself. So it doesn't make sense to constrain the collection to the scroll view -- subviews of a scroll view can be as big as they want to be. If you want to limit the size of the collection view, just set its width to whatever width you prefer.
The constraints in your post make the collection view fill the scroll view along both axes. It's not clear why you'd want to do that. I will assume you actually want something like the App Store, which has multiple horizontally-scrolling collections in a vertically-scrolling scroll view.
Auto layout has special behavior for scroll views. Go read Technical Note TN2154: UIScrollView And Autolayout.
So, a constraint between the edge of a scroll view and a descendant of the scroll view affects the scroll view's content size. A constraint between the edge of a scroll view and a view outside the scroll view affects the scroll view's frame.
You need to pin the collection view's left edge to the left edge of your top-level view (the superview of the scroll view), and the right edge similarly. You can't do that with a visual format. You'll need to explicitly create those constraints (or set them up in your xib or storyboard).