iOS笔记

scrollview用masnory布局

2021-08-18  本文已影响0人  狗蛋的春天

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 = @"官方回复";
    [_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 这里最下面放了一个webview,加载html标签,高度后面计算
    _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);//暂时给个高度
    }];

拿到html标签处理 ,

   [_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;  
  }

计算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)];

      }];
  }
上一篇 下一篇

猜你喜欢

热点阅读