TableView horizontal scrolling according to cell width - ios

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!

Related

Why there is no possibility of scrollview scrolled when added contents dynamically.?

I am using scroll view. In my scenario, When contents are added dynamically in content view of scroll view. The view height must be increased automatically and content is added to the bottom with scrolling functionality. I tried the following. There is no particular way of scroll functionality. I don't know why scrolling is much complicated in iOS. Why scroll view not work the way like table view scrolling, table view taking any number of amount and scrolls. Finding youtube videos too not working as it has fixed height in advance .
Inside your scrollview you must add a UIView. Add a height constraint to the UIView and programatically change the height of the UIView depending on the content you are adding to this UIView. This scrollview will work if the height is large.

ScrollView is Not Appearing

Reference: Add a ScrollView to existing View
I inserted a scroll view into an existing view and now my page is not appearing and I am not sure how to fix this. My scroll view is under the view so I do not understand why it is not displaying.
With auto-layout, the UIScrollView needs to be able to calculate its content size using the available constraints. This is often best accomplished by adding a UIView in the scroll view to act as the content view, rather than directly embedding UIControl subclasses. The content view can then be constrained to be equal width and/or equal height to the parent view of the scroll view. The variable height/width (depending on the scroll direction) of the content view can be calculated by fully constraining the widgets it contains.

Auto Layout: Set height View depends on other height View

I need to implement AutoLayout in my layout. Below are some screenshots:
1.
2.
The case is :
I have a ShinobiDataGrid in View and a UITableView . First image is first condition, in first picture I want the view have height as height View Controller.
The second and third picture is when there are available TableView . I want the TableView always is at the bottom of View Controller and the View is have height depends of height Table View.
I read this, this and all of reference on that but it's not working for me.
Edit
I read some article about Auto Layout and I get this below:
1.
2.
But in first picture, the the UIView still have a distance with main View. I want it only have 3 or 4 point from bottom of main View like below:
Now, I have one constrain from UIView to the UITableView. I add one constrain from UIView to bottom main View with custom priority, constant and etc but it's not working.
1.I would suggest you to use UITableView with section header and footers , with no scroll. Add your views to UITableViewCell. Populate your UITableView with custom cells based on your requirements.
2.You can also use UIStackView for the same. Add all your views & table views to a vertical stack view in Interface Builder. Keep distribution as fill equally. First time hide your UITableView. Your view will cover complete then show / hide tableview when required and update stack view.
Constraint your views & table views with respect to stack view. UIStackView manages autolayout.
3.I would have used UITableViewController with static cells (with no scroll) with views and tableviews as subview of static UITableViewCells. First time in heightFoRowAtIndexPath pass complete height. When tableview will be visible. Reload tableview controller and pass height accordingly.
Instead of using UITableView inside cell. I would suggest using UIStackView as subview cell. Show your data in UIStackView.
First add scrollview.
Add the height constraint of First View equal to view controller.
Below to that, add the tableview, get the dynamic height and change according to it. You can see my answer here for this.

UICollectionView - vertical scrollling, horizontal paging with custom layout

I'm trying to implement an iOS collection view which basically should work similar to a table view, but the cells' widths being integer multiples of the collection view's width (eg. 4 times the width). the collection view should scroll vertically (like a regular tableview does), but it should page horizontally across the content.
I do have the custom layout working except for the paging. Currently, the collectionView will just scroll horizontally. I'm unsure how to implement the paging correctly.
If I just set pagingEnabled on the collectionView, it has no effect. I suspect that the scrollview needs to be told what the width of a page is..?
Or do I have to do anything weird because there is only one cell/column per row, instead of multiple cells?
See the image; the grey bars are the cells, the blue rect is the collection view frame.
thanks!
I believe if the collection view item widths are less than or equal to the width of the collection view, then the horizontal paging should work via the pagingEnabled property. Otherwise, you need to create the paging effect yourself using the UIScrollView delegate methods and animating to offsets yourself after a certain threshold

How can I get a UICollectionView to fill the width of its containing scroll view with Auto Layout without going off the screen?

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).

Resources