gpt4 book ai didi

ios - 以编程方式设置页面控制但不执行任何操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:28 24 4
gpt4 key购买 nike

我正在尝试使用分页 UICollectionView 并尝试以编程方式设置当前页面,但它什么也没做。它总是从第 0 页开始。

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;


// Set up the collection view with no scrollbars, paging enabled
// and the delegate and data source set to this view controller
self.collectionView = [[UICollectionView alloc]
initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)
collectionViewLayout:flowLayout];

self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.pagingEnabled = YES;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;

[self.view addSubview:self.collectionView];


CGFloat w = self.view.frame.size.width;
CGFloat h = self.view.frame.size.height;

// Set up the page control
CGRect frame = CGRectMake(0, h - 60, w, 60);
self.pageControl = [[UIPageControl alloc]
initWithFrame:frame];

// Add a target that will be invoked when the page control is
// changed by tapping on it
[self.pageControl addTarget:self action:@selector(pageControlChanged:)
forControlEvents:UIControlEventValueChanged];

// Set the number of pages to the number of pages in the paged interface
// and let the height flex so that it sits nicely in its frame
self.pageControl.numberOfPages = self.products.count;
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.pageControl.currentPage=self.index;
[self.view addSubview:self.pageControl];
[self.collectionView registerClass:[DetailCustomCell class]
forCellWithReuseIdentifier:@"CELL_ID"];

最佳答案

这是在 ViewWilLAppear 中执行的正确方法。

CGFloat pageWidth = self.collectionView.frame.size.width;
CGPoint scrollTo = CGPointMake(pageWidth * self.index, 0);
[self.collectionView setContentOffset:scrollTo animated:NO];

关于ios - 以编程方式设置页面控制但不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16054051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com