美文网首页
UICollectionView相关

UICollectionView相关

作者: 喵喵粉 | 来源:发表于2019-11-26 14:27 被阅读0次

1. oc

#define kNavBarHeight44.0
#define kSafeBottom34 (kIsPhoneX ?34.f:0.f)
#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height

// 判断是否为iPhone X 系列  这样写消除了在Xcode10上的警告。
#define kIsPhoneX \
({BOOL isPhoneX = NO;\
if (@available(iOS11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom >0.0;\
}\
(isPhoneX);})
[self.cv mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(0);
    make.left.right.mas_equalTo(0);
    make.top.equalTo(self.view).offset(kStatusBarHeight+self.navigationController.navigationBar.frame.size.height+0);
    make.bottom.equalTo(self.view);//.offset(-kSafeBottom34-0);
}];
- (MyLayout*)myLayout{
    if(!_myLayout) {
        _myLayout = [MyLayout new];
        _myLayout.delegate = self;

        CGFloatinset =5;
        CGFloatspace =5;

        _myLayout.sectionInset=UIEdgeInsetsMake(inset, inset, inset+kSafeBottom34, inset);
        _myLayout.minimumInteritemSpacing = space;
        _myLayout.minimumLineSpacing= space;
    }
    return _myLayout;
}
  • 在每次collectionView的data(init delete insert reload)变化的时候、屏幕旋转的时候,都会调用这个方法准备布局
    override func prepare() {}

设置layout

/**
 减去collectionView.contentInset
 */
- (CGSize)collectionViewContentSize {
    NSLog(@"collectionViewContentSize:%f", _hLayout);
    
    return CGSizeMake(kScreenWidth-self.collectionView.contentInset.left-self.collectionView.contentInset.right, _hLayout);
}
  • 计算layoutAttribute.frame
    x: 距离cell.contentView的偏移,与collectionViewcontentInset没关系
    width: 需要减去每个sectioninsetcollectionViewcontentInset
attributes.frame = CGRectMake(sectionInset.left, _contentHeight, size.width - self.collectionView.contentInset.left - self.collectionView.contentInset.right - sectionInset.left - sectionInset.right, size.height);

获取cell的位置

  UICollectionViewLayoutAttributes * att = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
  CGRect cellRect = att.frame;
  CGRect cellFrame = [collectionView convertRect:cellRect toView:collectionView];

2. Swift

  • 1.自定义cell cell大小在属性的didSet里面设置或者在layoutSubviews设置
class PageTitleViewCvCell: UICollectionViewCell {

    @IBOutlet weak var lbName: UILabel!
    
    var name: String = "" {
        didSet {
            lbName.text = name
            lbName.frame = bounds
        }
    }
}

class PageTitleViewCvCell: UICollectionViewCell {

    @IBOutlet weak var lbName: UILabel!
    
    var name: String = "" {
        didSet {
            lbName.text = name
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        lbName.frame = bounds
    }
}
  • 2.默认选中
extension PageTitleView {
    
    fileprivate func getTitles() {
        cvClass.reloadData()
        
        ///默认选中第一个item
        let indexPath = IndexPath(row: 0, section: 0)
        cvClass.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionView.ScrollPosition())
        collectionView(cvClass, didSelectItemAt: indexPath)
    }
}
  • 3.获取cell的位置
let attributes = collectionView.layoutAttributesForItem(at: indexPath)
if let cellRect = attributes?.frame {
     let cellFrame = collectionView.convert(cellRect, to: collectionView)
            
     UIView.animate(withDuration: 0.25) {
          self.vLine.frame = CGRect(x: cellFrame.minX, y: cellFrame.maxY-kScrollLineH, width: cellFrame.width, height: kScrollLineH)
        }
}

相关文章

网友评论

      本文标题:UICollectionView相关

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