cell嵌套webView,cell高度自动调整 二

#import

@class BFCActCenterDetailModel;

@class BFCWonderfulModel;

@interfaceBFCWebScrollView :UIScrollView

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

@property (nonatomic, strong) BFCActCenterDetailModel *detailModel;

@property (nonatomic, strong) BFCWonderfulModel *wonderfullModel;

/**

 初始化无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{

    // 自动适配图片宽度

    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;

    // 自适应屏幕宽度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调用

    [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++;

    // 执行多次,但只做一次更改

//    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);

            }

        }

//    }

    // 执行五次后移除kvo 否则会一直执行

    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

//{

    // 没效果

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

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

//        return;

//    }

//}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容