Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust height Dynamically #86

Closed
annjawn opened this issue Feb 5, 2018 · 9 comments
Closed

Adjust height Dynamically #86

annjawn opened this issue Feb 5, 2018 · 9 comments

Comments

@annjawn
Copy link

annjawn commented Feb 5, 2018

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 this UILabel 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 that GSKStretchyHeaderView is always using the default maximumContentHeight (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 the maximumContentHeight dynamically based on the content of the Stackview inside the Xib? any help will be greatly appreciated.

@gskbyte
Copy link
Owner

gskbyte commented Feb 5, 2018

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.

@annjawn
Copy link
Author

annjawn commented Feb 5, 2018

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.😥

@gskbyte
Copy link
Owner

gskbyte commented Feb 6, 2018

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.

@annjawn
Copy link
Author

annjawn commented Feb 6, 2018

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.

@annjawn
Copy link
Author

annjawn commented Feb 6, 2018

@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.

@annjawn
Copy link
Author

annjawn commented Feb 6, 2018

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.

@annjawn
Copy link
Author

annjawn commented Feb 6, 2018

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. 🙁

@gskbyte
Copy link
Owner

gskbyte commented Feb 7, 2018

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...

@annjawn
Copy link
Author

annjawn commented Feb 7, 2018

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.

@gskbyte gskbyte closed this as completed Mar 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants