UITableView Content Size Issue not updating - ios

I am working with Swift 4.0 where I want UITableview with both vertical and horizontal scroll.
I have set contentSize of my UITableview. If I change the frame of UITableview programmatically then contentSize of UITableview is not working.
If the frame is same as it is set using autolayout then the contentSize of UITableview is working fine.
Below is the code I have tried :
let height : CGFloat = CGFloat((self.transactionArr.count*60)+70)
let width = self.view.frame.size.width*2
self.tblWidth.constant = self.view.frame.size.width
self.view.updateConstraintsIfNeeded()
self.tableView.contentSize = CGSize(width: width, height: height)

Don't hack UITableView - it is designed to scroll only vertically. Check out UICollectionView instead, it is designed for both vertical and horizontal scrolling (although you can make it scroll just one way if that's your need) - docs, or a tutorial.

You can put your tableview in a ScrollView. Make sure your ScrollView size is the same with tableview.
set your ScrollView width bigger than your container view and you can scroll your tableview horizontally.

Related

how to resize scroll view after remove subview swift?

I have subview in a scrollview and after I remove some subview from the bottom there is a blank space and the scroll view still keeps height, not resizing. How to fix this?
Scrollview's contentHeight & contentWidth is calculated from its subviews(if used auto layout). If autolayout is not used then you need to set its content size.
So whenever you are adding or removing subviews in scrollview you need to take care of its contentHeight and contentWidth.
If you're using auto layout, you need to add constraints to newly added view in such a way that scroll view can calculate its contentHeight from it.(in auto layouts you don't need to explicitly set the content height to scroll view)
If you're using frame base layout, you need to set the contentHeight explicitly to the scroll view. In frame base layout, you need to take care of scrollviews content size.
Why is there blank space in scrollview?
When a subview is removed from scrollview, its content size need to be adjusted. It means it is not able to calculate its content size.
you can use this line
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: (YOURVIEW) - (THEREMOVEDVIEW))
I recommend you that you must save the height before remove view for later you can adjust.
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself like below:
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)
note: change width and height as you want.
you can try this also:
let content: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = content.size

swift: how to adjust table view's height to fit super view's height

I have a viewController into which I dragged a tableView, but when I load data in the tableView I get a scroll, what I want is to extend this tableView to its height to fill its superView. I tried to resize the tableView by setting tableView.frame.height = tableView.contentSize.height in viewDidAppear and it worked but I don't get a scroll in the superView, the tableView just got expanded and the content is down the view but I cant scroll, I tried to put this tableView inside a scrollView but still the same thing, what can I do?
yourTableview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
Basically what you did was adjust the height of the tableView to be is the height if its content rather than the height of the screen (the content height is larger than the screen)
The table should carry the height of the screen (or any height you want) and if your content is bigger than the frame of your tableView it will automatically have the scrolling enabled.
Hope this helps!
If you want your scrollview to be scrollable, you need to indicate its contentSize by dynamically calculating the height of its nested components:
scrollView.isScrollEnabled = true
scrollView.contentSize = imageView.frame.height + tableview1.frame.height + tableview2.frame.height + netLabel.frame.height
Notice that you may also have to include your margins' height
I just needed to add the constraint of height of my table as an Outlet like this #IBOutlet weak var myConstraint: NSLayoutConstraint!, and the do myConstraint.constant = myTable.contentSize.height in viewDidAppear

Self sizing tableview

First of all this is not a question about how to automatically size the cells inside the tableview, moreover how to automatically resize the entire tableview.
So I have a scrollview which has a tableview and 2 other views as its subviews. The tableview's cells already automatically resize itself, again this question is not about the individual cells. However, the tableview does not resize at all.
What I have done:
1) Set up the tableview to have a top, bottom, leading and trailing constraint
2) Set the cells up to have auto layout enabled
3) * I do not know the cell size at build time
4) I have disabled scrolling mode on tableview
So long story short, how can I go along to get the tableview to resize itself?
Edit
The cells contain a label which can have various lines of text, so therefore the cells, which use auto layout, should then determine the height of the table view.
The following images show how the view is set up:
As you can see the tableview is only a small part of the view and since the scrollview from the tableview is deactivated there should, and aren't, any scrolling problems.
EDIT 2
This is actually how it should end however, i am calculating this on my own and everytime I want to make a small change to the cells the whole code, which calculates the height of the cell, needs to be rewritten and it is quite difficult for me to get the height just right.
Edit 3
Until now I had a height constraint on the tableview which I calculated manually, however removing this constraint and trying to let auto layout handle the tableview height size creates the following error:
Scroll View
Need constraint for: Y position or height
I can conclude therefore that the tableview does not know how to automatically calculate the height based on its cells with autolayout.
You don't need to create a height constraint or set frame whatsoever. Create a subclass of UITableView and recalculate its intrinsicContentSize every time its contentSize changes aka new data added or removed. Here is all you needed:
class SelfSizingTableView: UITableView {
override var contentSize: CGSize {
didSet {
invalidateIntrinsicContentSize()
setNeedsLayout()
}
}
override var intrinsicContentSize: CGSize {
let height = min(.infinity, contentSize.height)
return CGSize(width: contentSize.width, height: height)
}
}
You can change your UITableView's frame by using tableview.frame = CGRect(x: <some_x>, y: <some_y>, width: <some_width>, height: <some_height>)
If your UITableViewCells use auto layout then they should resize when the UITableView's frame changes.

UIScrollview inside subview Height Change Dynamicaly

I am facing a trouble with UIScrollView using auto layouts. I have the following view hierarchy with constraints.
ViewA (leading,trailing,top and bottom spaces to superview).
-- ViewB (leading,trailing,top and bottom spaces to superview).
--- UIScrollView (leading,trailing,top and bottom spaces to superview).
Here ViewB is adding on ViewA and i have some textfields,buttons and UITableview these all are placed inside UIScrollView.This is my view hierarchy.
Now i want to my Scrollview height increase dynamically based on tableview number of rows.
For this I'm using bellow code but it not working.Can any one please suggest me.
CGFloat height = MIN(self.view.bounds.size.height, placeTbl.contentSize.height);
self.Height_PlaceTbl.constant = height;
[self.view layoutIfNeeded];
if you want to increase your ScrollView height based on tableview number of rows you can try this
first get number of your tableview rows and then get height and width of your scrollview
let width = self.scrollview.layer.bounds.width
let height = self.scrollview.layer.bounds.height
after this , you can increase height of your scrollview like this
self.scrollview.frame = CGRectMake(0, 0, width, height + numberofrows)

UICollectionView - Scroll out of bounds

I have an UICollectionView let's say 200 width. I can scroll vertically without any problems. But I would like to be able to scroll the collectionView 60 px out of bounds to the right. Is this possible?
Sure it is, UICollectionView inherits from UIScrollView, that means that you can add an inset. This means that you will scroll more but the content size where you place your cell will be the same.
SWIFT var contentInset: UIEdgeInsets OBJECTIVE-C
#property(nonatomic) UIEdgeInsets contentInset Use this property to add to the
scrolling area around the content. The unit of size is points. The
default value is UIEdgeInsetsZero.
Remember also to add a scrollIndicatorInsets to balance the scroll bar movement.

Resources