CollectionView cells appearing outside collectionView. - ios

I have a collectionView laid out in the storyboard like so:
Note the constraint: Collection View.top = Search Bar.bottom
I don't want any of the content in the collection View above that line. However when the app runs and you scroll the collectionView, this is what happens to the cells. What could be happening here?

Probably collectionView.clipsToBounds = true

Related

What Kind of scroll view to contain 2 tableviews and collectionview

I have complex view that contains Image slider(collectionView),List of offers(tableView) and List of Services (TableView), The main Content View should be scrollable and all subviews should not be scrollable.
I tried to make the parent view UIScrollView but i got a problem that when the first tableview items exceeds the screen limit its not showing the exceeded elements.
I tried also to force full tableview height but i had a problem with scrolling
self.tableView.frame.size = CGSize(width: self.tableView.contentSize.width,
height: self.tableView.contentSize.height)
Is there a way to force showing all the tableview elements ,or should i use another type than UIScrollView?
Here is an image for what i want to display
For this you need to disable your UITableview scroll like:
tableView.scrollEnabled = false
then all you need to get count of tableview element multiply it by the height of your tableview cell and set that height to tableview like:
tableViewHeight.constant = count * z
Where z is height of tableview cell.
All set now.

Resize ScrollView based on TableView size

I'm trying to create layout that it structured like this:
- View
-- ScrollView
--- ContentView
---- CustomView
---- CustomView
---- TableView
---- CustomView
The tableView itself is auto-resizable using "invalidateIntrinsicContentSize" and when I add items - the height of the tableview changes, pushing the custom view below it further down.
Once enough items are added I the bottom custom view is hidden and the scroll doesn't work.
important fact - the bottom custom view doesn't have a bottom constraint. It is pushed down by the it's top constraint to the tableView.
If I do set a bottom constraint - the table view will no longer be dynamically resized.
The intended behaviour:
When a user adds items to the list and the list gets too big the ContentView will be scrollable so the user can scroll to see the bottom view.
The actual behaviour:
When a user adds items to the list and the list gets too big, the bottom view is pushed down and outside of sight and content is not scrollable.
What is happening and how can I fix it?
Below is what I think what is happening.
Since you are using UITableView, it has its own scroll view. So when the UITableView list gets too big, UITableView itself becomes scrollable rather than ScrollView's contentView becoming scrollable.
To achieve what you need, you would have to make the UITableView not scrollable and use the intrinsicHeight of the UITableView to get the actual height of UITableView along with all the items. If you have items with varying heights, it will be a problem because you won't know the height before rendering. With same height for all the rows, you can get the total height of the UITableView and set the height constraint to that value. This will increase the contentSize of the outer ScrollView, making it scrollable.
Apart from UITableView, you can also use UIStackView. This is because you are not using the reusing capabilities of UITableView anyways. Managing the datasource and delegates should not be a big problem.
You can create a constraint for tableview height, And take its reference to your swift file, by dragging it as you take other views. Now in your code, Just do this
tableViewHeightConstraint.constant = tableViewNoOfItems * tableViewCellHeight;
if you have set other constraints perfectly inside scrollview, It should work perfectly. Means TableView should have top, bottom, left, right margined constraints from the ScrollView.
try this code
tblViewHeight.constant = CGFloat( tableview row count * 45 )
var size = contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
if size.height < scrollView.frame.size.height
{
size = scrollView.frame.size
}
contenViewHeight.constant = size.height - scrollView.frame.size.height
scrollView.contentSize.height = contenViewHeight.constant
What I think you could do is:
Disable tableView's scroll tableView.isScrollEnabled = false
Every time a user adds items to the list, reload the tableView
Also using UIStackView with vertical axis and .fillEqually distribution as a Content View would be much more convenient as you won't need to set any positional constraints to your views, but may need to set height constraints if intrinsic content size can't be determined by the engine

TableView and CollectionView in one ViewContoroller

I have UITableView and UICollectionView in one ViewController.
If CollectionView is below the TableView in a hierarchy, the scrolling and clicking work only for CollectionView but for TableView all interactions are not working at all and vice verse...
On the screen, there is only one visible element TableView or CollectionView, and it's controlled by isHidden = true/false
What is wrong have I missed? How to have both TableView and CollectionView scrollable/clickable and manage that which is visible on the screen?
You need to manage isHidden for containers like
container1.isHidden = true / false
container2.isHidden = true / false
tableView/collectionView are just subviews of their containers so hiding/unhiding them doesn't make the ability to interact with back-most one

Auto set height of table view based on UICollectionView inside it

I have a UITableView with cells that have inside a UICollectionView.
Now all works great except of UICollectionViewCell scrolling i want that the height of table view cell follow the Collection view cell content.
In simple word i want scrolling in table view but not inside single cell i need to display all contents of UIcollectionViewCell without scrolling inside it.
Thanks
You have to calculate the collectionviews' heights and set a height constraint on each of them. then add top and bottom constraints from the collectionviews to their superviews (the tableviewcells / their contentviews) and let autolayout do its work.
don't forget
tableView.estimatedRowHeight = 44 // or something similar
tableView.rowHeight = UITableViewAutomaticDimension
in the controller's viewDidLoad to make the automatic row height calculation work.

Collection View overlaps above when scrolling vertically

So I have collectionView but above that a UIView as well. When I scroll my collectionView vertically and go up, it scrolls and overlaps the UIView.
What could be the solution to make the UIView come on top of collectionView?
What did work was turning off clips to bounds in the collection View from storyboard.
Set the property "Clip to Bounds" to true. It can be done programatically or in the auto layout.
Programatically: colelctionView.clipToBounds = true
Auto Layout:
Your constraints should be like,
View - top,leading,trailing and fixed height
collection view - top,leading,trailing,bottom
and after that setup, you got issue then once try to set below in viewDidload,
self.automaticallyAdjustsScrollViewInsets = false
Try keeping View Below the Collection View in the View Hierarchy.
This looks sneaky, what let us know the effect.
Just play with the Section Inset

Resources