Swift: UITableView scrolls back to top when section header height is changed dynamically - swift

!
I have a tableView with a single section. The header of this section holds the Sort By label and the three ImageViews. The view below it is a UITableViewCell.
The first image shows the initial view of the section header. When the header is clicked (have added a tap gesture), I expand the section view by changing the constant of the height constraint programmatically (have created an outlet for the height constraint).
Everything works fine, the section header expands as desired. However the tableviewcell scrolls right to the top again. Meaning, if when the section header was not expanded and I had scrolled down to some level, as soon as the header view expands, the entire offset is lost and tableviewcell scrolls right back to the top. I do not want this. I want the table view cell to remain where it is and for the section header to expand and collapse over it. How can I achieve this? Please help.

it sounds like you might be doing this:
setContentOffset(CGPointZero, animated: true)
which you shouldn't do.

Related

Scroll content over fixed background image in swift

I am trying to achieve a scrolling over a fixed image, as you can see on the picture.
I thought I should use a scrollview but I didn't quite get it how to use it and what to include into the scrollview, since the image and the button on the bottom should be fixed. In addition, the content should only be scrollable, when the text is to long.
I think the best solution here would be using a UITableView inside a UIViewController.
You can then set up the cells to make it look exactly the way you want.
For instance the first cell would just be the image, the second cell would be your title, the third one would be your menu bar, and so on.
This may be really useful if you plan to present dynamically the elements inside your UIViewController (for example if you need to use a database, you may want to animate the insertion of the rows only if the content has be downloaded already)
So using a UIViewController, you can just add the UITableView to it and set it up using contraints so that it fills the entire view. Then you can just add your button on the top of your table view, as a subview of your UIViewController's view (not as a subclass of your table view otherwise your button would end up scrolling too).
And again here you would need to add a few constraints to your button to make it look alright!
Just try it like this and let me know if you have any difficulties in the implementation :)
It is very complex to explain, To understand you must know good autolayout
To achieve that you need two scrollview and little math :)
Suppose your headerview height is 200
As you know when using autolayout we need following view hierarchy
Your view Hierarchy should be
--> Main View
--> ScrollView
--> Container View
-->Your HeaderView (200)
-->Content View (Equal height to UIView)
--> UIView (Your tabs like button , followbutton)
--> ScrollView 2
--> ContainerView
-->Subviews
Scroll view 1 Will used wo scroll the headerview and second scrollview will used to scroll other content of scrollview (Like Tab and text content as shown in picture )
Content View (Equal height to UIView) will allow your content view to give height of 200 extra to scroll bottom and your second scrollview will scroll to top which will allow to scroll your other content too
https://media.giphy.com/media/a2A4AQeAIkAhO/giphy.gif

section header for UICollectionView

I'm subclassing UICollectionViewLayout.
UITableView sectionHeader stays around when scrolled down.
(You can see this when you scroll down the contacts list, section header(A, B, C..) stays around until next section header takes its place.
I'd like to add a section header to my UICollectionView but don't know how to do this.

UITableView section appears over tableHeaderView

I have a UITableView instance to which I have added a header using the tableHeaderView property. I observe for changes in the contentOffset property to be able to move the header in the opposite direction, allowing it to stay at the top of the table when scrolled at least a certain amount.
I want to have it in this way because of two reasons. First, I want the scroll bar to cover the header. Secondly, I'm moving the header with the scrolled content for about 100 points, then I'm keeping it at the top.
Everything is fine except for one detail. The table view sections appear above the tableHeaderView. How do I get them to appear below?
EDIT: How do I get the sections of a UITableView to appear below the assigned tableHeaderView view in the view stack?
This is not a solution to the problem, but this way worked for me:
I simply pushed the titles into the array feeding the table view cells as strings and added them as cells with different cell identifiers than the cells i used otherwise.

UITableView with dynamically set size

I have a UITableView as a subview in a ScrollView with other widgets around like a button. I'd like to put the button always at the end of the ScrollView and I'd like to have the UITableView to show dynamically more section. How and where shall I determine the Table size, correctly set it and visualize it?
From Interface Builder it seems that I can only set static size to the TableView (which of course limits the number of sections visible) and stick the button position to the bottom whether a rotation happens.
If you have only few simple controls after the table then I'd suggest putting them to the table itself and get rid of the unhealthy (in my opinion) combination of table view inside scroll view.
You might add the button you are talking about to the table's footer.
It may be done in the Interface Builder (drag-n-drop the button to the bottom of the table view) or in the code ([tableView setTableFooterView:myButton];).
If your button should be smaller that table's width then put it inside UIView and locate as you need.
You can also add table header in a similar way...

When and where should I add a view to a UITableView's footer?

I am populating a UITableViewController's UITableView through code only. At the bottom of the table I wish to position a button that scrolls into view as the user scrolls to the bottom of the table.
When in the UITableViewController life cycle should I populate the table footer with a button? viewDidLoad?
p.s. I wish to avoid using section footers in the UITableView.
Yes, viewDidLoad is the correct place. It's not a stone-set rule though - I have change footer view in many different situations, such as after rotation in didRotateFromInterfaceOrientation.
Note that the view will be repositioned to location immediately below the last row, so if you want to provide a margin or centering for your button I suggest adding a plan UIVIew as footer, and then add your button(s) into that UIView.
Yes, put it in viewDidLoad. Here is some sample code.
You can just set the tableFooterView property of a UITableView to your button.