How do you make UITableHeaderView highly adaptive?

I believe that many iOS developers have some confusion when we need to implement some complex interface effects in UITableView, except for the UITableViewCell self-sizeing, which is usually thought of. In fact, UITableViewHeaderView is also able to make full use of. But to achieve some complex effects by UITableViewHeaderView, the largest crater is UITableView and not according to the height of the UITableViewHeaderView to adjust their height, for example: when you initialize a height of 100 headerView as UITableViewHeaderView, when you go through the code to set the height of the headerView is not with the. The solution here is to replace a new UITableViewHeaderView. This article will be combined with the author’s own project needs, and we discussed how to achieve a high degree of adaptive UITableViewHeaderView.

Take a look at the effect I have achieved in my project

How do you make UITableHeaderView highly adaptive?
IMG_3286.PNG
How do you make UITableHeaderView highly adaptive?
IMG_3288.PNG
How do you make UITableHeaderView highly adaptive?
IMG_3287.PNG

My page layout is a UITableView. The entire page layout is divided into two sets. The first set is note taking. The second set is note taking. The notes contain rich text, pictures, attachments, and other views. If all of them are implemented in UITableViewCell, that’s fine, and the notes are processed as a separate Cell. But when the View of the notes is UIWebView, things are not as simple as that.

First of all, if you use UITableViewCell to embed a UIWebView, it’s very cumbersome to handle. Because you have to consider the problem of high callback. Because as long as the network is bad, UIWebView may be loaded all the time, and UITableView can’t get the exact height. And when you need to process views that aren’t just UIWebView, they’re more troublesome. So, in general, this page layout recommends using UITableViewHeaderView to accomplish layout best.

For example, first, split the views in the contents of the note: rich text view, picture view, attachment view. The note reviews are highly adaptive cell. Inside

By changing some of the adaptive heights of the view, changing UITableHeaderView is not working. For example, similar to the article in Jane’s article, the layout of the details of the interface, my idea is the whole is a UITableView, the article is headerView, the list of comments as a cell. The project I’ve been developing recently is similar to the effect of Jane’s book, and I’m using UITableView to do it.

In fact, the most difficult this project layout is not highly adaptive UITableHeaderView, only on the Internet to see a lot of people in this difficult problem, write down a way to solve it, and then again the difficulty in my program, I hope you can help the people to have the same problem.

First of all, I need to put a lot of things in headerView, for example, what time the author information and publish articles, this is a view, and then the contents of the article, this is a UIWebView, and finally some to some beautiful design of the control chart, here as a view to see.

If you just need to display a web page without having to load the local HTML file, it’s very simple to get the height of the page directly in the – (void) webViewDidFinishLoad: (UIWebView *) webView method. This method I mentioned in the last edition of the iOS rich text editor. After you’ve reached the height, you can complete the layout directly through the following code. It is no use setting height directly.

UIView *view=self.mainTableView.tableHeaderView; view.frame = CGRectMake (0,0, ScreenW, self.webView.height+56+30+52+20); self.mainTableView.tableHeaderView = view;

If you need to show “is a local HTML file in the CSS file (which may be linked to CSS may be the local CSS file, but this does not affect), you need to let UIWebView load the local HTML file, and then let it show to show the contents of the problem, webViewDidFinishLoad in the first method execution will load the HTML file, then unless you reload”, this method does not execute the. And, even if you’re in the reload page, you’ll find that the height you get is still the height it sets when it is initialized. How do you get the height of the webView load? At this point you need a powerful KVO to help you! The – (void) webViewDidFinishLoad: (UIWebView *) webView increases the listening to the webView:

(if! Self.isObserving) {[self startObservingHeight];} - {[self.webView.scrollView (void) startObservingHeight addObserver:self forKeyPath:@ "contentSize" options:NSKeyValueObservingOptionNew context:nil]; self.isObserving = YES;} - {[self.webView.scrollView (void) stopObservingHeight removeObserver:self forKeyPath:@ "contentSize" context:nil]; self.isObserving = NO;} - (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (ID) object change: (NSDictionary< NSString id> * *, change) context: (void * context) {if ([keyPath isEqualToString:@ "contentSize"]) {NSLog (@ "% @ [change objectForKey:@, monitor the changes of" "new" "); / / NSStringFromCGSize ([change objectForKey:@" new "]); self.webView.height = self.webView.scrollView .contentSize.height; UIView *view=self.mainTableView.tableHeaderView; view.frame = CGRectMake (0,0, ScreenW, self.webView.height+56+30+52+20); [self.webView mas_updateConstraints:^ (MASConstraintMaker *make) {make.left.equalTo (self.headerView).Offset (20); make.right.equalTo (self.headerView).Offset (-20); make.top.equalTo (self.headView.mas_bottom); make.height.equalTo (@ (self.webView.height));}]; self.mainTableView.tableHeaderView = view;}}

This will make it work.
this method dynamically listens to the height of the webView and then completes the adaptation through the automatic layout.