iOS 随笔

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZFHomeAdvertCell" bundle:nil] forCellWithReuseIdentifier:@"ZFHomeAdvertCell"];


    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(400, 60);
    self.collectionView.collectionViewLayout = flowLayout;
}



#pragma mark - 代理
#pragma mark--UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZFHomeAdvertCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZFHomeAdvertCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    
    
   
    return cell;
}
- (IBAction)tttt:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    
    for(UICollectionViewCell *cell in self.collectionView.visibleCells){
        if ([cell isKindOfClass:[ZFHomeAdvertCell class]]) {
            ZFHomeAdvertCell *homeAdvertCell = (ZFHomeAdvertCell *)cell;
            
            if (homeAdvertCell.collectionView.visibleCells.count) {
                NSLog(@"homeAdvertCell.collectionView.visibleCells.count:%ld",homeAdvertCell.collectionView.visibleCells.count);
                
                if (sender.selected) {
                    
                    self.alert = [EmptyAlert msGetInstance];
                    [self.collectionView addSubview:self.alert];
                    //redview在垂直方向上距离边界为50
                    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:self.alert attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.collectionView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
                    
                    self.alert.translatesAutoresizingMaskIntoConstraints = NO;
                    
                    
                    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:self.alert attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.collectionView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
                    
                    
                    //设置redview的宽
                    //设置blueview和redview等宽
                    NSLayoutConstraint *constraint5 = [NSLayoutConstraint constraintWithItem:self.alert attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.collectionView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
                    //设置blueview和redview等高
                    NSLayoutConstraint *constraint6 = [NSLayoutConstraint constraintWithItem:self.alert attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.collectionView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
                    
                    [self.collectionView addConstraint:constraint1];
                    [self.collectionView addConstraint:constraint2];
                    [self.collectionView addConstraint:constraint5];
                    [self.collectionView addConstraint:constraint6];
                }else{
                    [self.alert removeFromSuperview];
                }
                
                
                
                
                
            };
            break;
            
            
        }
    }
    
    
}

你可能感兴趣的:(iOS 随笔)