【问题标题】:The offset of grid view(UICollectionView) shift gradually with paging scroll网格视图(UICollectionView)的偏移量随着分页滚动而逐渐移动
【发布时间】:2017-12-25 09:33:11
【问题描述】:

我正在使用 UICollectionView 创建网格视图,并希望通过分页滚动它。

现在我有一个问题。通过分页滚动,网格的偏移量逐渐移动

我想使下一页单元格的左上角适合屏幕的左上角。

我的代码如下,

ViewController.m

- (void)loadView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    self.view = [[GridView alloc] initWithLayout:layout];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.navigationBarHidden = YES;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

GridView.m

#define GRID_SPACE (20)

- (id)initWithLayout:(UICollectionViewFlowLayout *)layout
{
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        self.pagingEnabled = YES;

        self.backgroundColor = UIColor.redColor;

        self.delegate = self;
        self.dataSource = self;

        [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
    }
    return self;
}

#pragma mark -
#pragma mark UICollectionViewDelegate

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize selfSize = self.frame.size;
    return CGSizeMake( (selfSize.width - GRID_SPACE) / 2, (selfSize.height - GRID_SPACE) / 2 );
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return GRID_SPACE;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return GRID_SPACE;
}

如果您能给我一个好的解决方案,我将不胜感激。

谢谢。

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    UIScrollView(UICollectionView 继承自)的页面大小是其视口的大小,因此要使其正常工作,您需要在顶部包含红色边框的高度。如果您不想在顶部或底部显示边框,那么您可以让 UICollectionView 拉伸 under 另一个元素,或者如果它在顶部对齐,则可以拉伸到显示器的顶部下方。

    【讨论】:

    • 对不起,我听不懂你在说什么。你能告诉我需要设置的代码或确切的属性名称吗?
    • 我试过你的建议,它解决了我的问题。谢谢。
    【解决方案2】:

    LGP 的回答解决了我的问题。我添加到我的 GridView.m 的已解决代码如下,

    将grid item的长度加上CollectionView的高度,因为高度就是Paging Scroll的长度。 此外,您需要将网格项的长度设置为 CollectionView insets 的底部。

    在 GridView.m 中

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        CGSize parentSize = self.superview.frame.size;
    
        self.frame = CGRectMake(0, 0, parentSize.width, parentSize.height + GRID_SPACE);
    }
    
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    {
        return UIEdgeInsetsMake(0, 0, GRID_SPACE, 0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 2014-06-13
      相关资源
      最近更新 更多