scrollview用masnory布局

viewDidLayoutSubviews

    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];

        CGFloat contentY = CGRectGetMaxY(self.bottomView.frame);
        if (contentY < CGRectGetMaxY(self.view.bounds)) {
        contentY = CGRectGetMaxY(self.view.bounds);
      }
      [self.mainScrollView setContentSize:CGSizeMake(ScreenWidth, contentY)];
    }

viewdidload里面

    _mainScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_mainScrollView];
    _mainScrollView.showsVerticalScrollIndicator = NO;
    [_mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    _mainScrollView.contentSize = CGSizeMake(ScreenWidth, 1000);

    _topBagView = [[UIView alloc]initWithFrame:CGRectZero];
    [_mainScrollView addSubview:_topBagView];
    _topBagView.backgroundColor = [UIColor colorWithHexStr:@"#70AAAA" alpha:0.2];
    _topBagView.layer.cornerRadius = Set_WidthScale(4);
    _topBagView.layer.borderColor = [UIColor colorWithHexStr:@"#70AAAA" alpha:0.06].CGColor;
    _topBagView.layer.borderWidth = 0.5;
    [_topBagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mainScrollView).mas_offset(Set_WidthScale(16));
        make.width.mas_equalTo(Set_WidthScale(343));
        make.top.equalTo(_mainScrollView).mas_offset(Set_WidthScale(12));
    }];
//
    _contentLb = [[UILabel alloc]init];
    _contentLb.font = [UIFont systemFontOfSize:Set_WidthScale(16)];
    _contentLb.textColor = [UIColor colorWithHexStr:@"#99999C"];
    _contentLb.numberOfLines = 0;
    [_topBagView addSubview:_contentLb];

    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Set_WidthScale(12));
        make.left.mas_equalTo(Set_WidthScale(8));
        make.width.mas_lessThanOrEqualTo(Set_WidthScale(327));
    }];

     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemWidth = (ScreenWidth - Set_WidthScale(16)*2 - Set_WidthScale(12)*4) / 3;
    itemWidth = floorf(itemWidth);
    layout.itemSize = CGSizeMake(itemWidth, itemWidth);
    layout.minimumLineSpacing = Set_WidthScale(12);
    layout.minimumInteritemSpacing = Set_WidthScale(12);

    _imageCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [_topBagView addSubview:_imageCollectionView];
    _imageCollectionView.backgroundColor = UIColor.clearColor;
    _imageCollectionView.delegate = self;
    _imageCollectionView.dataSource = self;

    [_imageCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentLb.mas_bottom).mas_offset(Set_WidthScale(20));
        make.bottom.mas_equalTo(self.topBagView.mas_bottom);
        make.left.equalTo(self.topBagView);
        make.right.equalTo(self.topBagView);
        make.height.mas_equalTo(10);
    }];

    [_imageCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];

    _bottomView = [[UIView alloc]init];
    [self.mainScrollView addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Set_WidthScale(16));
        make.width.mas_equalTo(self.topBagView);
        make.top.mas_equalTo(self.topBagView.mas_bottom).mas_offset(Set_WidthScale(8));
    }];

    _tipLb = [[UILabel alloc]init];
    _tipLb.font = [UIFont boldSystemFontOfSize:Set_WidthScale(16)];
    _tipLb.textColor = [UIColor colorWithHexStr:@"#353634"];
    _tipLb.text = @"官方回復(fù)";
    [_bottomView addSubview:_tipLb];
    [_tipLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bottomView);
        make.top.mas_equalTo(Set_WidthScale(12));
    }];

    _timeLb = [[UILabel alloc]init];
    _timeLb.font = [UIFont systemFontOfSize:Set_WidthScale(12)];
    _timeLb.textColor = [UIColor colorWithHexStr:@"#9B9C9A"];
    [_bottomView addSubview:_timeLb];
    [_timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bottomView);
        make.centerY.mas_equalTo(self.tipLb);
    }];

    //webview 這里最下面放了一個(gè)webview,加載html標(biāo)簽,高度后面計(jì)算
    _wkWebV = [[WKWebView alloc]init];
    [_bottomView addSubview:_wkWebV];
    [_wkWebV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bottomView);
        make.top.mas_equalTo(self.tipLb.mas_bottom).mas_offset(Set_WidthScale(12));
        make.bottom.mas_equalTo(Set_WidthScale(-12));
        make.width.mas_lessThanOrEqualTo(Set_WidthScale(343));
        make.height.mas_equalTo(200);//暫時(shí)給個(gè)高度
    }];

拿到html標(biāo)簽處理 ,

   [_wkWebV loadHTMLString:[self getStrings:backStr] baseURL:nil];

  - (NSString *)getStrings:(NSString *)str{
      NSString *tmp = [NSString stringWithFormat:@"<body width=320px style=\"word-wrap:break-word; font-family:Arial\"><p     style='padding-top:0.0px;'></p ><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-    scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style'     content='black'><meta name='format-detection' content='telephone=no'><style type='text/css'>img{width:%f</style>%@<p     style='padding-bottom:0.0px;'></p>",kScreenWidth - 20,str];
      return tmp;  
  }

計(jì)算webview高度

  - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
        [webView evaluateJavaScript:@"document.body.scrollHeight"
                completionHandler:^(id _Nullable result,NSError *_Nullable error) {
        // 高度
        CGFloat scrollViewHeight = [result doubleValue];
        DGLog(@"-------%f",scrollViewHeight);
        [self->_wkWebV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.bottomView);
            make.top.mas_equalTo(self.tipLb.mas_bottom).mas_offset(Set_WidthScale(12));
            make.bottom.mas_equalTo(Set_WidthScale(-12));
            make.width.mas_lessThanOrEqualTo(Set_WidthScale(343));
            make.height.mas_equalTo(scrollViewHeight);
        }];
        CGFloat contentY = CGRectGetMaxY(self.bottomView.frame);
        if (contentY < CGRectGetMaxY(self.view.bounds)) {
            contentY = CGRectGetMaxY(self.view.bounds);
        }
        [self.mainScrollView setContentSize:CGSizeMake(ScreenWidth, contentY + scrollViewHeight)];

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

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

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