将CollectionView单元格与Autolayout对齐以适用于所有设备

时间:2017-09-24 17:18:29

标签: ios xcode uicollectionview autolayout

我对xCode 9的约束有问题。 我有一个带有UIView的UIVIewController,它包含一个显示当月日期的UICollectionView,一切都很完美,但是我在各种设备上的单元格都有问题......

collectionView使用分页进行单元格滚动

在这里你可以看到在iPhone 8上加上它没问题,但在iPhone上如果切割了细胞.. enter image description here

我为我的collectionView设置了UICollectionViewFlowLayout但仍有问题...可能是错误的约束但我无法理解这些约束中的哪一个是错误的。

以下是我用于自定义单元格的约束的图像 enter image description here

以下是我放在UIViewController中的自定义UIView的约束

enter image description here

我还会向您展示我使用过的代码,也许错误就在这里

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder: aDecoder];
    if (self) {

    }
    return self;
}

-(void)setupContent:(UIView *)controller {

    _arrayWeek = [[NSMutableArray alloc] init];
    _arrayDay = [[NSMutableArray alloc] init];

    UICollectionViewFlowLayout *smartLayout = [[UICollectionViewFlowLayout alloc] init];
    _smartCalendar = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:smartLayout];

    //////// Collection View ////////
    _smartCalendar.delegate = self;
    _smartCalendar.dataSource = self;
    _smartCalendar.backgroundColor = [UIColor clearColor];
    _smartCalendar.pagingEnabled = YES;

    UINib *nib = [UINib nibWithNibName:@"UDSmartCalendarCell" bundle: nil];
    [_smartCalendar registerNib:nib forCellWithReuseIdentifier:cellID];

    _smartCalendar.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_smartCalendar];

     //////// CONSTRAINT COLLECTION VIEW ////////
    [_smartCalendar.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:0.0].active = YES;
    [_smartCalendar.topAnchor constraintEqualToAnchor:self.topAnchor constant:0].active = YES;
    [_smartCalendar.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
    [_smartCalendar.heightAnchor constraintEqualToConstant:60].active = YES;

     //////// FLOW LAYOUT COLLECTION VIEW////////
    smartLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    CGFloat width = floorf(_smartCalendar.frame.size.width  / 6);
    smartLayout.itemSize = CGSizeMake(width, 50);
    smartLayout.minimumInteritemSpacing = 3;
    smartLayout.minimumLineSpacing =3;
    smartLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _smartCalendar.collectionViewLayout = smartLayout;
    _smartCalendar.showsHorizontalScrollIndicator = NO;


    [self buildSmartCalendarWithWeekArray:_arrayWeek andDay:_arrayDay];
    [self scrollToItemToday];

}

0 个答案:

没有答案