美文网首页
UIScrollView+UIStackView

UIScrollView+UIStackView

作者: 冷武橘 | 来源:发表于2022-12-08 11:17 被阅读0次

    stackView作为ScrollView的滚动内容,arrangedSubview的最右边因为填充边缘的,进而来确定滚动范围

             let scrollview = UIScrollView()
             view.addSubview(scrollview)
             scrollview.translatesAutoresizingMaskIntoConstraints = false
             scrollview.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
             scrollview.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
             scrollview.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
             scrollview.heightAnchor.constraint(equalToConstant: 200).isActive = true
             scrollview.backgroundColor = .red
            
             let stackview = UIStackView()
             stackview.spacing = 20;
             scrollview.addSubview(stackview)
             stackview.translatesAutoresizingMaskIntoConstraints = false
             stackview.trailingAnchor.constraint(equalTo: scrollview.trailingAnchor).isActive = true
             stackview.topAnchor.constraint(equalTo: scrollview.topAnchor).isActive = true
             stackview.leadingAnchor.constraint(equalTo: scrollview.leadingAnchor).isActive = true
            
            for _ in 0 ... 5 {
                let view = UIView()
                view.backgroundColor = .green
                stackview .addArrangedSubview(view)
                view.translatesAutoresizingMaskIntoConstraints = false
                view.widthAnchor.constraint(equalToConstant: 100).isActive = true
                view.heightAnchor.constraint(equalToConstant: 100).isActive = true
            }
    
    • configureScrollview
    - (void)configureScrollView:(UIScrollView *)scrollView AddArrangedSubview: (void(^)( UIStackView *stackView))block  {
        UIStackView *stackView =[[UIStackView alloc]init];
        [scrollView addSubview:stackView];
        stackView.backgroundColor =[UIColor yellowColor];
        [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(scrollView);
            make.height.equalTo(scrollView.mas_height);
        }];
        block(stackView);
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UIScrollView *scrollView =[[UIScrollView alloc]init];
        [self.view addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.offset(100);
            make.right.offset(-100);
            make.height.mas_equalTo(300);
        }];
        scrollView.backgroundColor =[UIColor redColor];
      
        [self configureScrollView:scrollView AddArrangedSubview:^(UIStackView *stackView) {
            for (int i=0; i<6; i++) {
                UIView *redView =[[UIView alloc]init];
                redView.backgroundColor = [UIColor grayColor];
                [stackView addArrangedSubview:redView];
                stackView.spacing =20;
                [redView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(@200);
                }];
            }
        }];
    }
    

    相关文章

      网友评论

          本文标题:UIScrollView+UIStackView

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