ios – 如何将集合单元格宽度设置为动态拉伸到手机宽度
2020年02月04日
作者:佚名
如何设置集合单元视图以动态拉伸到
iphone屏幕宽度(例如
iphone 5s,iphone 6 plus)?
我试过了:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { (ResultCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellClass forIndexPath:indexPath]; cell.bounds = CGRectMake(0,self.view.bounds.size.width,150); return cell; }
这不起作用.我没有看到内容延伸到屏幕的右侧.
我试过添加这个委托方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize cellSize; cellSize.width = self.view.bounds.size.width; // body view height cellSize.height = 150; return cellSize; }
我在方法中设置了断点,但该方法永远不会被调用?
解决方法
我对同一个用例有同样的问题.我终于结束了
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(collectionView.bounds.size.width,150); }
这会更改集合视图的每个项目(单元格)的大小.因此,您可以使用此更改每个单元格的大小.在这里,我需要单元格宽度与我的UICollectionView相同,所以我传递了UICollectionview的宽度和我想要的特定高度.希望这会有所帮助.
此外,没有必要在 – (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中设置单元格边界,因为大小通过 – (CGSize)collectionView设置:(UICollectionView *)collectionView布局:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
还要确保您已连接所需的代表和DataSources到UICollectionView
以上是来客网为你收集整理的ios – 如何将集合单元格宽度设置为动态拉伸到手机宽度全部内容,希望文章能够帮你解决ios – 如何将集合单元格宽度设置为动态拉伸到手机宽度所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。