iOS ---masonry設(shè)置scrollview的子控件

今天寫項(xiàng)目,頁面如下,在scrollview上舉報(bào)選項(xiàng),我是先在scrollview上添加一個(gè)底部控件(backView),然后再在backView上添加按鈕。并且backView顏色為白色。使用masonry布局后,backView顏色不顯示,并且使用調(diào)試工具查看不到backView。如果使用frame設(shè)置,就可以設(shè)置顏色,調(diào)試工具也可以看到backView。


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

正如,我們用xib布局scrollView一樣,添加scrollView后,我們還要添加一個(gè)view到scrollView上,設(shè)置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也一樣需要添加一個(gè)view,上下左右,寬高。都要和scrollview一致。然后再在view上添加子控件即可。這是因?yàn)閟crollview的left,right, top,bottome是基于contentsize,而不是bounds。而contentsize基于子控件,所以需要先添加一個(gè)子控件

- (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:@"可以對其他內(nèi)容進(jìn)行補(bǔ)充說明" 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];
}
?著作權(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)容