cell嵌套webView,cell高度自動(dòng)調(diào)整 二

#import

@class BFCActCenterDetailModel;

@class BFCWonderfulModel;

@interfaceBFCWebScrollView :UIScrollView

@property(nonatomic,copy)void(^scrollViewBlock)(CGFloatheight);

@property (nonatomic, strong) BFCActCenterDetailModel *detailModel;

@property (nonatomic, strong) BFCWonderfulModel *wonderfullModel;

/**

?初始化無(wú)title的界面

?@param frame 尺寸

?*/

- (instancetype)initWithNotitleFrame:(CGRect)frame;

@end



#import "BFCWebScrollView.h"

#import "BFCActCenterDetailModel.h"

#import "BFCWonderfulModel.h"

#import

#import "NSObject+RACKVOWrapper.h"

@interface BFCWebScrollView ()<WKUIDelegate, WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, strong) UILabel *headerLabel;

@property (nonatomic, assign) CGFloat webViewHeight;

@property (nonatomic, assign) CGFloat index;

@property (nonatomic, assign) BOOL showTitle;

@end

@implementationBFCWebScrollView

- (UILabel*)headerLabel{

? ? if (!_headerLabel) {

? ? ? ? _headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 17, Main_Screen_Width, 20)];

? ? ? ? _headerLabel.text=@"圖文詳情";

? ? ? ? _headerLabel.textColor=RGB(74,74,74);

? ? ? ? _headerLabel.textAlignment = NSTextAlignmentCenter;

? ? ? ? _headerLabel.font = FONT(@"PingFangSC-Medium", 15);

? ? ? ? [selfaddSubview:_headerLabel];

? ? }

? ? return _headerLabel;

}

- (instancetype)initWithFrame:(CGRect)frame{

? ? if(self== [superinitWithFrame:frame]) {

? ? ? ? self.showTitle=YES;

? ? ? ? [self createWebView];

? ? ? ? self.webView.frame = CGRectMake(0, CGRectGetMaxY(self.headerLabel.frame)+10, Main_Screen_Width, 100);

? ? ? ? [RACObserve(self, detailModel)subscribeNext:^(BFCActCenterDetailModel*detailModel) {

? ? ? ? ? ? if(detailModel.details!=nil) {

? ? ? ? ? ? ? ? if([detailModel.detailshasPrefix:@"http"]) {

? ? ? ? ? ? ? ? ? ? [selfloadWebPage:detailModel.details];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else{

? ? ? ? ? ? ? ? ? ? [selfloadHtmlString:detailModel.details];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }];

? ? }

? ? return self;

}

- (instancetype)initWithNotitleFrame:(CGRect)frame{

? ? if(self== [superinitWithFrame:frame]) {

? ? ? ? self.showTitle=NO;

? ? ? ? [self createWebView];

? ? ? ? self.webView.frame=CGRectMake(0,0,Main_Screen_Width,TAMargin);

? ? ? ? [RACObserve(self, wonderfullModel)subscribeNext:^(BFCWonderfulModel*wonderfullModel) {

? ? ? ? ? ? if(wonderfullModel.content!=nil) {

? ? ? ? ? ? ? ? if([wonderfullModel.contenthasPrefix:@"http"]) {

? ? ? ? ? ? ? ? ? ? [selfloadWebPage:wonderfullModel.content];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else{

? ? ? ? ? ? ? ? ? ? [selfloadHtmlString:wonderfullModel.content];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }];

? ? }

? ? return self;

}

/**

?加載http

?*/

- (void)loadWebPage:(NSString*)urlStr {

? ? NSURL *url = [NSURL URLWithString:urlStr];

? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];

? ? [self.webViewloadRequest:request];

}

/**

加載HTML字符串

?*/

- (void)loadHtmlString:(NSString*)string{

? ? // 自動(dòng)適配圖片寬度

? ? NSString *header = @"img{max-width:100% !important;display:block;}";

? ? NSString*newStr = [NSStringstringWithFormat:@"%@%@",header,string];

? ? [self.webView loadHTMLString:newStr baseURL:nil];

}

/**

?加載本地HTML文件

?*/

- (void)loadLocalFile:(NSString*)localFile {

? ? NSURL *url = [[NSBundle mainBundle] URLForResource:localFile withExtension:nil];

? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];

? ? [self.webViewloadRequest:request];

}

- (void)createWebView

{

? ? WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];

? ? WKUserContentController *wkUController = [[WKUserContentController alloc] init];

? ? wkWebConfig.userContentController= wkUController;

? ? // 自適應(yīng)屏幕寬度js

? ? NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

? ? WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

? ? // 添加js調(diào)用

? ? [wkUControlleraddUserScript:wkUserScript];


? ? self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];

? ? self.webView.backgroundColor = [UIColor clearColor];

? ? self.webView.opaque=NO;

? ? self.webView.userInteractionEnabled = NO;

? ? self.webView.scrollView.bounces = NO;

? ? self.webView.UIDelegate = self;

? ? self.webView.navigationDelegate = self;

? ? [self.webView sizeToFit];

? ? [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];


//? ? [[self.webView.scrollView rac_valuesForKeyPath:@"contentSize" observer:self] subscribeNext:^(NSString x) {

//? ? ? ? //X 修改了? NSSize

//? ? ? ? NSLog(@"%@",x);

//? ? }];

//? ? [[self.webView.scrollView rac_valuesAndChangesForKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew observer:self] subscribeNext:^(id x) {

//? ? }];

? ? [self addSubview:self.webView];

}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context

{

? ? self.index++;

? ? // 執(zhí)行多次,但只做一次更改

//? ? if (self.webViewHeight == 0) {

? ? ? ? if([keyPathisEqualToString:@"contentSize"]) {

? ? ? ? ? ? UIScrollView*scrollView = (UIScrollView*)object;

? ? ? ? ? ? CGFloatheight = scrollView.contentSize.height;

? ? ? ? ? ? self.webViewHeight= height;

? ? ? ? ? ? if(self.showTitle) {

? ? ? ? ? ? ? ? self.webView.frame=CGRectMake(0,CGRectGetMaxY(self.headerLabel.frame) +10,Main_Screen_Width, height);

? ? ? ? ? ? }

? ? ? ? ? ? else{

? ? ? ? ? ? ? ? self.webView.frame=CGRectMake(0,0,Main_Screen_Width, height);

? ? ? ? ? ? }

? ? ? ? ? ? self.contentSize=CGSizeMake(Main_Screen_Width, height);

? ? ? ? ? ? if(self.scrollViewBlock) {

? ? ? ? ? ? ? ? self.scrollViewBlock(height);

? ? ? ? ? ? }

? ? ? ? }

//? ? }

? ? // 執(zhí)行五次后移除kvo 否則會(huì)一直執(zhí)行

? ? if(self.index>=5) {

? ? ? ? [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];

? ? }

}

- (void)dealloc

{

? ? [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];

? ? [self.webView stopLoading];

}

//-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error

//{

? ? // 沒(méi)效果

//? ? [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];

//? ? if (error.code==-999) {

//? ? ? ? return;

//? ? }

//}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容