Ios 网格视图(UICollectionView)的偏移量随着分页滚动逐渐移动

Ios 网格视图(UICollectionView)的偏移量随着分页滚动逐渐移动,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我正在使用UICollectionView创建网格视图,并希望通过分页滚动它 现在我有一个问题。网格的偏移量通过分页滚动逐渐移动 我想把下一页单元格的左上角调整到屏幕的左上角 我的代码如下所示 ViewController.m - (void)loadView { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UIC

我正在使用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;
}
#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;
}
- (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);
}
GridView.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;
}
#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;
}
- (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);
}
如果你能为我提供一个好的解决方案,我将不胜感激


谢谢。

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

LGP的回答解决了我的问题。下面是我添加到我的GridView.m中的已解析代码

将网格项的长度添加到CollectionView的高度,因为该高度是页面滚动的长度。 此外,还需要将网格项的长度设置为CollectionView插图的底部

在GridView.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;
}
#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;
}
- (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);
}

对不起,我听不懂你说的话。您能告诉我需要设置的代码或确切的物业名称吗?我尝试了您的建议,解决了我的问题。非常感谢。