I have an issue at the moment I am trying to put two table views inside a scrollview in one controller and these tableview are placed one below another. these two tableview uses scrollview for scrolling.
so I used vertical stackview inside scrollview. but when I create cell, both tableview height is not increases as well as scrollview is not able to scroll.
How should i use scrollview scroll for scrolling tableview?
-- scrollview
-----VerticalStackView
--------Tableview 1
--------Tableview 2
I'm really lost with this.Any help will be greatly appreciate it.
You need to make each UITableView define it's own size based on their content. To do that subclass both of them using the class below.
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
Then, for each UITableView you need to set isScrollEnabled = false. Otherwise their defined size will be 0.
Then just add each table view to the stack view you're using inside the scrollview. If their combined height is larger than the screen height, it'll scroll.
Related
I am trying to zoom in and out a TableView. The TableView is in a View which is inside a ScrollView. The TableView is scrollable but the pinch to zoom isn't working like it works with ImageView.
I would really appreciate if anyone could shed some light to this problem and share a working example.
I have already tried setting min and max zoom for UIScrollView in attribute inspector.
You can do that by:
first you have to make your table view content size intrinsic by make it subclass the below class.
class IntrinsicTableView: UITableView {
override open var contentSize: CGSize {
didSet{
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize{
return contentSize
}
}
Then you will stop scrolling of your table view from the storyboard.
Then you will configure scroll view to enable zooming.
That's it.
I found the next answer to make UIView's height match its content https://stackoverflow.com/a/39527226/7767664
I tested it, it works fine (if UIView height size in storyboard bigger or smaller than its content then during runtime it autoresize itself to match the content).
But if I use UICollectionViewCell instead of UIView then nothing changes, height of cell is never changed, it always has the hardcoded height we have in storyboard properties:
What else can I do?
Also I have 2 sections in UIControllerView (2 columns).
Two cells in one row should have the same size even if their size content is different (something like this implemented in Android natively when using RecyclerView with GridLayoutManager, very easy)
Update
It works with UIView because I set its top constraint to Safe Are'a top
I can't do it with UICollectionViewCell
Update 2
It seems I have some progress with this answer https://stackoverflow.com/a/25896386/7767664
But instead of newFrame.size.width = CGFloat(ceilf(Float(size.width))) I need newFrame.size.height = CGFloat(ceilf(Float(size.height)))
and when we use this solution, don't add any constraints to cell's bottom otherwise it will not work
With this solution I can't really use any margins otherwise some part of becomes invisible at the bottom of cell
I guess it can be solved with this question https://stackoverflow.com/a/31279726/7767664
You can try with this function called inside you collectionView Extension or inside your native collectionViewController class:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
//return something like the size. I write you an example how to use it.
// You can easily change the value according to your stuff contents height.
return CGSize(width: collectionView.frame.size.width/3, height: 100)
}
I solved my issue thanks to https://stackoverflow.com/a/25896386/7767664 and https://stackoverflow.com/a/31279726/7767664
I decided to put one UIView with all needed child views inside it to UICollecitonViewCell
I set UIView trailing, leading, top to ICollecitonViewCell but I didn't set bottom to cell view (you should use the latest child view inside UIView and connect their bottoms, not with the cell view)
Then I added reference of UIView to my custom cell class and I use its height for cell's height:
public class MyCustomItemCell: UICollectionViewCell {
// other child views references ...
#IBOutlet weak var wrapperView: UIView! // view which contains your other views
//forces the system to do one layout pass
var isHeightCalculated: Bool = false
override public func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
//Exhibit A - We need to cache our calculation to prevent a crash.
if !isHeightCalculated {
setNeedsLayout()
layoutIfNeeded()
var newFrame = layoutAttributes.frame
newFrame.size.height = wrapperView.frame.height // use height of our UIView
layoutAttributes.frame = newFrame
isHeightCalculated = true
}
return layoutAttributes
}
}
I am having a tableview in which the row height is dynamic as per its UILabel content (using UITableViewAutomaticDimension). Now, I need a 15 point border around the table view, that also changes as per the tableview height.
I tried two approaches -
Took a UIView and placed the tableView on top of it with constraints
as 15 on each side (top, bottom, left and right).
Added four UIViews around the tableView with 0 constraint for each
view to the tableView. (so that the views are always attached to the
tableView).
In both the approaches, the bottom view (or bottom area) is always giving a space in towards tableView bottom.
And to achieve the dynamic height to the tableView, I am using the below code -
open override func viewDidLayoutSubviews() {
if isPopup! {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height + 190)
tableView.reloadData()
}
}
Here are my Storyboard and the result on the simulator -
How to make the bottom UIView always be attached with the tableView bottom, so it looks like a border around the tableView?
Use your first approach and a custom table view subclass that uses its contentSize to determine its height. That way you can remove the code in your viewDidLayoutSubviews and should be good to go.
class AutoHeightTableView: UITableView {
override var contentSize: CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return CGSize(width: UIView.noIntrinsicMetric,
height: contentSize.height)
}
}
I have a scroll view and inside of if couple labels and a tableView. I would like for that tableView to be scrolled by a outer scrollView and not the tableView's scrollView, so what I did is to set constraint for tableView height to be equal to contentSize height. But I have this problem that it is sized correctly only when push animation is completed (and viewDidLayoutSubviews gets called, I guess)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableHeightConstraint?.constant = tableViewController.tableView.contentSize.height
}
Video Link
Content size will refresh with scroll view logic. So layout subviews is surely not enough. I have one case where I resize table view depending on it's content. What I do is use intrinsic size (it is that compression priority thing in storyboard). I subclass the table view and override these:
override var contentSize:CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}
The rest may then be done with constraints. Im my case I do not resize it further then it's superview but you do what you must. Still a bit of caution here: If this is not restricted you destroy the table view dequeuing feature and all the cells may be loaded instantly which may consume loads of memory and CPU. I would avoid that if possible. And if not, the next best thing is using a vertical stack view on a scroll view which should produce the same result you seem to expect.
I have in my View a long UIScrollView (about 1000 px in height), and at the end of this UIScrollView I have a UITableView.
The cellForRowAtIndexPath is never called (surely i checked the delegate and datasource if they are connected right, and the IBOutlet of the table is strong) but numberOfRowsInSection is getting called.
I tried reloading the TableView when the UIScrollView scrolls so when the table is at focus the cellForRowAtIndexPath might get called, but with no luck.
Did anyone encounter a similar behaviour when trying to use tableview and scrollview together?
Your hierarchy is like this:
A parentView is there. Inside the parent view there is a scroll view and there is a table view. So, your tableview is somewhere at 1000 from origin of parentview.
So, tableview will never become visible to your parentview and no delegates will be fired.
Include your view as a headerView of your UITableView like that:
Update for 2020, swift 5.X. To create a custom UITableView that allows your tableview inside a scrollview!
1) Create a subclass of UITableView:
import UIKit
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
2) Add a UITableView to your layout and set constraints on all sides. Set the class of it to ContentSizedTableView.
3) You should see some errors, because Storyboard doesn't take our subclass' intrinsicContentSize into account. At runtime it will use the override in our ContentSizedTableView class