美文网首页iOS 技巧
iOS ---masonry设置scrollview的子控件

iOS ---masonry设置scrollview的子控件

作者: iOS程序媛ing | 来源:发表于2021-03-05 10:01 被阅读0次

今天写项目,页面如下,在scrollview上举报选项,我是先在scrollview上添加一个底部控件(backView),然后再在backView上添加按钮。并且backView颜色为白色。使用masonry布局后,backView颜色不显示,并且使用调试工具查看不到backView。如果使用frame设置,就可以设置颜色,调试工具也可以看到backView。


截屏2020-11-26 下午1.18.18.png

正如,我们用xib布局scrollView一样,添加scrollView后,我们还要添加一个view到scrollView上,设置view的约束
view.top = scrollView.top,
view.left = scrollView.left,
view.right = scrollView.right,
view.bottom = scrollView.bottom。
并且view.width = scrollView.width
view.height = scrollView.height

使用masonry也一样需要添加一个view,上下左右,宽高。都要和scrollview一致。然后再在view上添加子控件即可。这是因为scrollview的left,right, top,bottome是基于contentsize,而不是bounds。而contentsize基于子控件,所以需要先添加一个子控件

- (void)setupView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *backView = [[UIView alloc] init];
    [scrollView addSubview:backView];
    backView.backgroundColor = [UIColor clearColor];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.height.width.equalTo(scrollView);
    }];
    
    UIView *btnView = [[UIView alloc] init];
    [backView addSubview:btnView];
    btnView.backgroundColor = [UIColor whiteColor];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(backView);
    }];
    

    NSArray *array = @[@"晒单图片与商品不符", @"非法欺诈", @"黄赌毒暴力政治", @"广告灌水", @"其他"];
    CGFloat x = 20;
    CGFloat y = 21;
    CGFloat height = 21;
    CGFloat spaceV = 17.5;
    for (NSInteger i = 0 ; i < array.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btnView).offset(y);
            make.height.mas_equalTo(height);
            make.left.equalTo(btnView).offset(x);

        }];
        if (i == array.count - 1) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(btnView).offset(y);
                make.height.mas_equalTo(height);
                make.left.equalTo(btnView).offset(x);
                make.bottom.equalTo(btnView).offset(-20);
                
            }];
        }
        if ((i + 1) % 2 == 0) {
            x = 20;
            y += height + spaceV;
        } else {
            x  = kScreenWidth - 120 - 21;
        }
        [btn addTarget:self action:@selector(choose:)];
        [btn initWithTitle:array[i] font:15 fontWight:UIFontWeightRegular color:@"323232" selectColor:@"" image:@"单选-未选择"];
        [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:7.5];
        [btn setImage:[UIImage imageNamed:@"单选-已选择"] forState:UIControlStateSelected];
    }
    
    UIView *textViewBackView = [[UIView alloc] init];
    [self.view addSubview:textViewBackView];
    textViewBackView.backgroundColor = [UIColor whiteColor];
    [textViewBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView);
        make.top.equalTo(btnView.mas_bottom).offset(10);
        make.centerX.equalTo(scrollView);
        make.height.mas_equalTo(KSuitFloat(187.5));
    }];
    
    UITextView *textView = [[UITextView alloc] init];
    self.textView = textView;
    textView.delegate = self;
    textView.backgroundColor = [UIColor whiteColor];
    [textViewBackView addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView);
        make.top.equalTo(btnView.mas_bottom).offset(10);
//        make.centerX.equalTo(scrollView);
        make.width.mas_equalTo(kScreenWidth);
        make.bottom.equalTo(textViewBackView).offset(-30);
    }];
    
    UILabel *placeHolderLabel = [[UILabel alloc] initWithTitle:@"可以对其他内容进行补充说明" font:14 fontWight:UIFontWeightMedium color:@"959a9f"];
    self.placeHolderLabel = placeHolderLabel;
    [textView addSubview:placeHolderLabel];
    [placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textView).offset(16);
        make.left.equalTo(textView).offset(20);
    }];
    
    UILabel *numberLabel = [[UILabel alloc] initWithTitle:@"120字" font:11 fontWight:UIFontWeightRegular color:@"959a9f"];
    
    [textViewBackView addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(textViewBackView).offset(-11.5);
        make.bottom.equalTo(textViewBackView).offset(-6.5);

//        make.right.bottom.equalTo(textViewBackView);
 
    }];
    [numberLabel sizeToFit];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [scrollView addSubview:submitBtn];
    submitBtn.backgroundColor = RedColor;
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textViewBackView.mas_bottom).offset(20);
        make.height.mas_equalTo(@45);
        make.left.equalTo(scrollView).offset(20);
        make.centerX.equalTo(scrollView);
    }];
    [submitBtn addTarget:self action:@selector(submit)];
    [submitBtn initWithTitle:@"提交" font:17 fontWight:UIFontWeightMedium color:@"ffffff" selectColor:@"" image:@""];
    [submitBtn addBorderWithWidth:0 corner:22.5];
}

相关文章

网友评论

    本文标题:iOS ---masonry设置scrollview的子控件

    本文链接:https://www.haomeiwen.com/subject/dwbaiktx.html