iOS:uicollectionView的一个项目空间是1px而另一个是2px,我想要两个全部1px?

时间:2016-02-18 14:05:53

标签: ios uicollectionview

我有一个collectionView,在1和2部分我希望每个itemSpace和lineSpace都是1px,但第二个itemSpace总是2 px,这是截图,我已经设置了collectionView的backgroundColor红色,它看起来更清晰。

这是截图

enter image description here

我将minimumLineSpacing,minimumInteritemSpacing和itemSize设置为:

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
        {
            float sortaPixel = 1.0/[UIScreen mainScreen].scale;

            NSLog(@"minimumLineSpacing%f", sortaPixel);

            return sortaPixel;
        }

        - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
        {
            float sortaPixel = 1.0/[UIScreen mainScreen].scale;

            NSLog(@"minimumInteritemSpacing%f", sortaPixel);

            return sortaPixel;
        }

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if(ISBIND){
            return CGSizeMake(SCREEN_WIDTH, finalTopheight);
        }
        return CGSizeMake(SCREEN_WIDTH, defaultTopheight);
    }


    //This is the calculation process
    float sortaPixel = 1.0/[UIScreen mainScreen].scale;

    NSLog(@"sizeForItem%f", sortaPixel);

    CGFloat itemW = (self.collectionView.frame.size.width - sortaPixel * 2)/3.0;

    CGFloat itemH = 106;

    return CGSizeMake(itemW, itemH);
}

任何人都可以帮助我,给我一些想法,我非常感激。

2 个答案:

答案 0 :(得分:1)

  

这里必须描述问题和一点点的组合   后来的想法让我意识到,虽然   minimumInteritemSpacing是一个CGFloat传递它是没有意义的   未定义小数值及其执行操作时的行为。

     

这是因为,在点对点比例为1:1的设备上,   没有半像素这样的东西。它就是这样发生的   在某些情况下,它表现得像我预期的那样,从而使我蒙蔽了真实   (而且很明显)问题。

希望阅读本文可以帮助其他人快速意识到错误。

请参阅帖子了解Float和CGFloat,

Changing an Int to a CGFloat in Swift to return heightForRowAtIndexPath tableview function

答案 1 :(得分:0)

// AdselecterNamed top border in a view
+ (void)addTopBorderWithColor:(UIView *)view color:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin];
    border.frame = CGRectMake(0, 0, view.frame.size.width, borderWidth);
    border.tag = 1000;
    [view addSubview:border];
}



// Add bottom border in a view
+ (void)addBottomBorderWithColor:(UIView *)view color:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
    border.frame = CGRectMake(0, view.frame.size.height - borderWidth, view.frame.size.width, borderWidth);
    border.tag = 1001;
    [view addSubview:border];
}

// Add left border in a view
+ (void)addLeftBorderWithColor:(UIView *)view color:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    border.frame = CGRectMake(0, 0, borderWidth, view.frame.size.height);
    [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin];
    border.tag = 1002;
    [view addSubview:border];
}

// Add right border in a view
+ (void)addRightBorderWithColor:(UIView *)view color:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin];
    border.frame = CGRectMake(view.frame.size.width - borderWidth, 0, borderWidth, view.frame.size.height);
    border.tag = 1003;
    [view addSubview:border];
}