Closed annjawn closed 6 years ago
Hi,
maybe you can hack it like this: compute the ideal height for one line and then, every time the content of that label changes, set the contentHeight
of the header view to be contentHeightForOneLineHeight
+ labelHeightForOneLine
- currentLabelHeight
.
So my Xib's class calls an API to load the data into it's subviews and obviously that call is asynchronous. Which means that I might still be receiving data while the tableView.addSubview(header)
already get's executed in my viewcontroller. Prior to using this library, I was using the same Xib to load into the tableView's tableHeaderView
and once added there I would trigger a re-layout on the tableHeaderView
which would re-size everything appropriately using the label's sizeToFit()
and layoutIfNeeded()
, and it works great. However, the same approach isn't working here. GSKStretchyHeaderView
doesn't seem to be using the tableView's tableHeaderView
so can you tell me how I can access the stretchyHeaderView's view once it's added onto the tableView?
Also, will setting the maximumContentHeight
after the header is added to the tableview work? (so far it doesn't seem like it does). So even if I were to somehow calculate the label line height as you suggest, that calculation would be completely pointless if I am unable to assign it to the maximumContentHeight
. What is really needed for this to work is somehow being able to trigger another layout pass perhaps using a delegate. The other way would really be to simply remove and re-add the headerview again, but I need to test this theory.
I really want this library to work for me but all the headers in my app have dynamic content as I explain above so it's going to be really difficult to use this without ugly hacks.😥
Hi,
well, you will need to keep a reference to the header view in the view controller or wherever. When you change the maximumContentHeight
, you may want to check if you are at the top of the table view, and then scrollToTop()
manually.
I would try experimenting with some user-triggered height change, like tapping some button that adds text synchronously to your label in order to test this approach. Once you get it to work, you may add your code reacting to asynchronous calls.
Yes. The table is at its original position when I try to change the maximumContentHeight. I will try your suggestion in my dev environment today.
@gskbyte so here is what I found. With a button which adds 10 pts to the maximumContentHeight
, the height gets increased but it only takes affect if I drag the tableview/scrollview down and release. I tried firing a layout pass after adding on to the maximumContentHeight
but it didn't work.
OK I think I found something that does work (somewhat). I was able to expand the header using stretchyHeader.setMaximumContentHeight(300, resetAnimated: true)
. The 300 is just a static hard coded value but my next step is to find the custom UIView
's final height after all data has been populated.
Alright, I give up. It won't work in this scenario, i.e. if the header view XIB has dynamic content especially in a stackview and uses autolayout. I think if we get the capability of doing tableView.tableHeaderView = stretchyHeader
instead of tableView.addSubview(stretchyHeader)
then the tableview will respect the autolayout inside of the header XIB + we can then easily trigger a layout pass and utilize setMaximumContentHeight
to resize the header (i tried assigning to tableHeaderView
but it didn't work).
With the current addSubView
method there is no way to determine the height from the XIB, the only way it could work if I set translatesAutoresizingMaskIntoConstraints
of the stretchy header to false
and then have to manually add constraints which could get very ugly very quickly. 🙁
Well, if the only thing changing the height of the header view is the amount of text, then you can use that every time the height changes to set the maximumContentHeight manually...
Yes that's how I finally ended up doing. Calculate the label height using the boundingRect
and then adding (or removing) the offset to the default header height using setMaximumContentHeight
. It's working perfectly.
I really like this library, and so far it is working great with one small issue. I have a Xib that I use for the header view. The Xib has a vertical Stackview with one element as a
UILabel
. Now thisUILabel
gets data from API and is a multi line label. There could 1, 2,3 lines or no text at all. The Stackview also uses auto layout in the Xib and is pinned to the top, bottom, left. What I am noticing is thatGSKStretchyHeaderView
is always using the defaultmaximumContentHeight
(240) no matter what the content of the Stackview is, which is resulting in gaps being left in the header. Is there a way to calculate themaximumContentHeight
dynamically based on the content of the Stackview inside the Xib? any help will be greatly appreciated.