CollectionView水平滚动在ios 7中不工作

CollectionView水平滚动在ios 7中不工作,ios,Ios,CollectionView水平滚动在ios 7中不工作,但在ios 8和9中工作正常。有什么解决办法吗 代码如下: UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.itemSize = CGSizeMake(330, 100); coll

CollectionView水平滚动在ios 7中不工作,但在ios 8和9中工作正常。有什么解决办法吗

代码如下:

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.itemSize = CGSizeMake(330, 100);

collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, 350, 100) collectionViewLayout:layout];
collection.delegate = self;
collection.dataSource = self;
collection.backgroundColor = [UIColor greenColor];
collection.pagingEnabled = YES;
[collection registerClass:[UICollectionViewCell类]forCellWithReuseIdentifier:@“Cell”]


它现在适合我的需要,并且可能对其他人有用,因此下面是我的代码: 我在iOS7-8-9中进行了测试。下面的例子是默认的iPad。您只需要更改目标设置和O.S版本

请下载并让我们知道您的意见。快乐编码

如果要将集合视图添加到视图中,请使用以下代码。下面的代码是我以编程方式创建的CL视图,它也可以与Autolayout和iOS 7-8-9一起正常工作。请执行以下代码,并让我们知道您的意见

.H文件

@interface HorizontalCollectionViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>{

    NSIndexPath *visibleIndexPath;
}
@property (nonatomic, assign) NSInteger selectedCell;

@property (strong, nonatomic) IBOutlet UICollectionView *horizonCLView;
#import "HorizontalCollectionViewController.h"
static NSString * const CellIdentifier = @“horizonCell";

@interface HorizontalCollectionViewController ()

@end

@implementation HorizontalCollectionViewController

@synthesize horizonCLView;


- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"view frame : %@",NSStringFromCGRect(self.view.frame));


    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];
    [self viewDidLayoutSubviews];


    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = self.view.frame.size;
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [layout setMinimumInteritemSpacing:0.f];
    [layout setMinimumLineSpacing:0.f];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);


    horizonCLView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    horizonCLView.dataSource = self;
    horizonCLView.delegate = self;
    horizonCLView.backgroundColor = [UIColor lightGrayColor];
    horizonCLView.scrollEnabled = YES;
    [horizonCLView setBounces:NO];
    [horizonCLView setUserInteractionEnabled:YES];
    [horizonCLView setPagingEnabled:YES];
    [horizonCLView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    [self.view addSubview:horizonCLView];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

-(void)viewWillAppear:(BOOL)animated{


    //Select Current Image For Sare In Social Media
   // currentImageIndex = selectedCell;

    [horizonCLView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:selectedCell inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

    CGRect visibleRect = (CGRect){.origin = self.horizonCLView.contentOffset, .size = self.horizonCLView.bounds.size};
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
    visibleIndexPath = [self.horizonCLView indexPathForItemAtPoint:visiblePoint];

  //  NSLog(@"visibleIndexPath.row in View will appear %ld",(long)visibleIndexPath.row);

    //SelectedCell is a selected image from gallary view
    NSLog(@"selectedCell in View will appear: %ld",(long)selectedCell);


}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [dataAry count];
}

#pragma Here we set the frame of horizontal scrll view
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    // expand cell to fill the entire view
    return collectionView.frame.size;

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    self.title = [[self.dataAry objectAtIndex:indexPath.row] valueForKey:@“dataAryName"];

    //imageVI.image = nil;

    imageVI = (UIImageView *)[cell.contentView viewWithTag:100];
    if (!imageVI){

        imageVI = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];
        imageVI.contentMode = UIViewContentModeScaleAspectFill;
        imageVI.tag = 100;
        imageVI.clipsToBounds = YES;
        [cell.contentView addSubview:imageVI];

    }

    dispatch_async(dispatch_get_main_queue(), ^{

        [asSetLib assetForURL:[NSURL URLWithString:[[self.dataAry objectAtIndex:indexPath.row] valueForKey:@"dataAryName"]] resultBlock:^(ALAsset *asset) {

            // imageVI.image = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
            imageVI.image = [UIImage imageWithCGImage:asset.defaultRepresentation.fullResolutionImage];
            NSLog(@"Preview view into album loaded successfully!");

        } failureBlock:^(NSError *error) {
            NSLog(@"An error occurred while loading image: %@", error.description);
        }];

    });


    return cell;

}

我在添加collectionView后使用了一行代码,它在ios 7中也可以正常工作

在[self addSubview:self.collectionView]之后添加此代码

代码如下:

[自执行选择器:@selector(update)with object:nil afterDelay:1]

  • (作废)更新{ [self.collectionView setContentOffset:CGPointMake(1,0)]; }

    • Sudha,我认为代码中的这个问题不是iOS7问题。 在这里,我回顾了ur代码中的两个bug

      1) 在这一行中,您已经给出了集合视图的静态宽度

      collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, 350, 100) collectionViewLayout:layout]; 
      
      而不是看起来像

      collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width, 100) collectionViewLayout:layout];
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
          layout.itemSize = CGSizeMake(self.view.bounds.size.width+10, 100);
          [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
      
          collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width, 100) collectionViewLayout:layout];
          collection.delegate = self;
          collection.dataSource = self;
          collection.backgroundColor = [UIColor greenColor];
          collection.pagingEnabled = YES;
          collection.scrollEnabled = YES;
          collection.userInteractionEnabled = YES;
          [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
      
          [self.view addSubview:collection];
      
      }
      
      -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
          return 1;
      }
      
      -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
          return 1;
      }
      
      -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
      
          UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
      
          return  cell;
      
      }
      
      
      Happy Coding!
      
      确保集合视图宽度大小始终与视图大小一致

      2) 确保布局项的宽度大于集合视图的宽度。否则,滚动不起作用

      layout.itemSize = CGSizeMake(self.view.bounds.size.width+10, 100);
      
      3) 小心使用,

       - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
      
      collectionview布局大小方法的委托方法

      4) 最后你的代码看起来像

      collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width, 100) collectionViewLayout:layout];
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
          layout.itemSize = CGSizeMake(self.view.bounds.size.width+10, 100);
          [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
      
          collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width, 100) collectionViewLayout:layout];
          collection.delegate = self;
          collection.dataSource = self;
          collection.backgroundColor = [UIColor greenColor];
          collection.pagingEnabled = YES;
          collection.scrollEnabled = YES;
          collection.userInteractionEnabled = YES;
          [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
      
          [self.view addSubview:collection];
      
      }
      
      -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
          return 1;
      }
      
      -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
          return 1;
      }
      
      -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
      
          UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
      
          return  cell;
      
      }
      
      
      Happy Coding!
      

      你能在这里分享你的代码吗?如果我使用的是视图控制器,它工作正常。当我在视图滚动条上显示它不工作时,您正在scollview内添加collectionview???@Sudha,请查看编辑的答案并让我们知道您的评论!不,我没有使用滚动视图。在UIViewPlease share for iphone上添加集合视图,因为ios 7的问题在于我的代码正在运行fine@Sudha,它是通用的。如果你有下载,然后改变项目设置。让它成为iPhone。下面的代码也是通用的,并且可以完美地与iOS7配合使用。好吧,我找到了解决问题的方法。我添加了一行代码:[自执行选择器:@selector(update)with object:nil afterDelay:1];-(void)使用此代码后更新{[self.collectionView setContentOffset:CGPointMake(1,0)];}在ios 7中也可以使用此代码。但是如果我使用[self.collectionView setContentOffset:CGPointMake(1,0)];它马上就要崩溃了。知道吗?但是如果我使用[self.collectionView setContentOffset:CGPointMake(1,0)];直接在集合视图中,然后它崩溃了。我正在以与您相同的方式进行操作。它在ios 8和ios 9中工作,但在ios 7中不工作。我得到了解决方案,我也发布了答案。@Sudha,你能和我分享你的演示项目吗?我认为您的解决方案不正确。layout.itemSize=CGSizeMake(self.view.bounds.size.width+10100);也不会使集合单元格滚动我正在sdk中实现集合视图。我已经发布了我的问题的解决方案。