UIWebViewで表示されているコンテンツの下にフッターとしてUIViewを追加する実装をしなければいけなったためメモ。
調べてもスクロールしない固定フッターの情報ぐらいしかなかなかなかったので。
参考:https://github.com/coryalder/DMAFWebViewController
フッターの作成
@property (strong, nonatomic) UIView *footerView;
- (void)createFooter {
self.footerView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
}
ScrollViewのコンテンツサイズを監視
[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
KVOはdeallocで開放してください
- (void)dealloc {
[self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}
コンテンツサイズの変更にあわせてフッターを追加
#import <objc/runtime.h>
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentSize"] && self.footerView) {
NSNumber *incumbentheight = objc_getAssociatedObject(object, "associated_height");
NSValue *newValue = [change objectForKey:NSKeyValueChangeNewKey];
CGSize newSize;
[newValue getValue:&newSize];
if (!incumbentheight || [incumbentheight floatValue] != newSize.height) {
CGFloat newHeight = newSize.height + self.footerView.frame.size.height;
if (!self.footerView.superview) {
[self.webView.scrollView addSubview:self.footerView];
} else if ([self.footerView superview] != self.webView.scrollView) {
[self.footerView removeFromSuperview];
[self.webView.scrollView addSubview:self.footerView];
}
self.footerView.frame = (CGRect){0,newSize.height, .size = self.footerView.frame.size};
objc_setAssociatedObject(object, "associated_height", @(newHeight), OBJC_ASSOCIATION_COPY);
self.webView.scrollView.contentSize = (CGSize){newSize.width, newSize.height+self.footerView.frame.size.height};
} else {
objc_setAssociatedObject(object, "associated_height", @(newSize.height), OBJC_ASSOCIATION_COPY);
}
}
}