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的偏移,与collectionView
的contentInset
没关系
width: 需要减去每个section
的inset
和collectionView
的contentInset
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
}
}
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)
}
}
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)
}
}
网友评论