如何解決UICollectionView bounces無(wú)效問(wèn)題

#import "WJ_VoteViewController.h"
#import "WJ_VoteCollectionViewCell.h"
#import "Masonry.h"


@interface WJ_VoteViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *mainCollectionView;
}

@property(nonatomic, assign) CGFloat itemWidth;

@end

@implementation WJ_VoteViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.itemWidth = (IPHONE_WIDTH - 5 * 10) / 4;
    
    [self setNavUI];
    
    [self setBottomUI];
}


//設(shè)置導(dǎo)航欄
-(void)setNavUI
{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backItem];
    self.title = @"下周菜單投票";
}


//導(dǎo)航欄下面的UI
-(void)setBottomUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //設(shè)置collectionView滾動(dòng)方向
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //設(shè)置headerView的尺寸大小
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
    
    //2.初始化collectionView
    mainCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:mainCollectionView];
    //大view的背景圖
    mainCollectionView.backgroundColor = [UIColor clearColor];
    mainCollectionView.bounces = YES;
    //3.注冊(cè)collectionViewCell
    //注意,此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致 均為 cellId
    [mainCollectionView registerClass:[WJ_VoteCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //注冊(cè)headerView  此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致  均為reusableView
    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.設(shè)置代理
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;

}


#pragma mark collectionView代理方法
//返回section個(gè)數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

//每個(gè)section的item個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WJ_VoteCollectionViewCell *cell = (WJ_VoteCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
//    cell.botlabel.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
    
    cell.backgroundColor = [UIColor blueColor];
    
    return cell;
}

//設(shè)置每個(gè)item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.itemWidth, 94);
}

//footer的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(10, 10);
}

//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(10, 10);
}

//設(shè)置每個(gè)item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//設(shè)置每個(gè)item水平間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


//設(shè)置每個(gè)item垂直間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}




//點(diǎn)擊item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WJ_VoteCollectionViewCell *cell = (WJ_VoteCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    NSString *msg = cell.botlabel.text;
//    NSLog(@"%@",msg);
}

#pragma mark - 監(jiān)聽(tīng)自定導(dǎo)航欄按鈕事件
-(void)navBtnClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}
collectionView點(diǎn)擊bounces.gif

從運(yùn)行效果上看垂直方向并不能滾動(dòng),快快想個(gè)解決辦法呀!
還好大金哥幫我想了一個(gè)辦法
mainCollectionView.bounces = YES;換成這句
mainCollectionView.alwaysBounceVertical = YES;

效果出來(lái)如圖所示:

collectionView點(diǎn)擊bounces1.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容