简体   繁体   中英

CollectionView Horizontal scroll not working in ios 7

CollectionView horizontal scroll not working in ios 7 but it's working fine in ios 8 and 9. Is there any solution for this ?

The Code is here:

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 class] forCellWithReuseIdentifier:@"Cell"];

[self.view addSubview:collection];

It now fits my needs, and could be useful for someone else, so here is my code: I have tested In iOS7-8-9. Below example is for Default iPad. You Just need to change target settings and OS Version.

https://github.com/philippeauriach/fullyhorizontalcollectionview

Please download it & let us know ur comments. Happy Coding!

If you want to add collection view onto the View then use below code. Below code into i have created CL View programmatically and It's also working correctly with Autolayout and iOS 7-8-9. Please implement below code and let us know ur comments.

.H File

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

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

@property (strong, nonatomic) IBOutlet UICollectionView *horizonCLView;

.M File

#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;

}

Sudha, I think this issues in your code not an iOS7 issues. Here, I have review two bug in ur code.

1)In this line you have give static width of collection view,

collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, 350, 100) collectionViewLayout:layout]; 

Instead Of it will be goes look like,

collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width, 100) collectionViewLayout:layout];

Make sure your collection view width size always go with according to your view size.

2)Make sure, Your layout item size width higher then collection view width frame. Otherwise Scrolling was not effected.

layout.itemSize = CGSizeMake(self.view.bounds.size.width+10, 100);

3) Carefully use,

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

delegate method of collectionview layout size method.

4)Finally your code look like,

- (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!

I've used a line of code after adding collectionView on view that its working fine in ios 7 also.

add this code after [self addSubview:self.collectionView];

here the code:

[self performSelector:@selector(update) withObject:nil afterDelay:1];

  • (void)update { [self.collectionView setContentOffset:CGPointMake(1, 0)]; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM