如何解決UICollectionView bounces無效問題

#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)聽自定導(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;

效果出來如圖所示:

collectionView點(diǎn)擊bounces1.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蒿偎,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子怀读,更是在濱河造成了極大的恐慌诉位,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件菜枷,死亡現(xiàn)場(chǎng)離奇詭異苍糠,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)啤誊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門岳瞭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蚊锹,你說我怎么就攤上這事瞳筏。” “怎么了牡昆?”我有些...
    開封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵姚炕,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我丢烘,道長(zhǎng)柱宦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任播瞳,我火速辦了婚禮掸刊,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赢乓。我一直安慰自己忧侧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開白布骏全。 她就那樣靜靜地躺著苍柏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪姜贡。 梳的紋絲不亂的頭發(fā)上试吁,一...
    開封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼烛恤。 笑死余耽,一個(gè)胖子當(dāng)著我的面吹牛碟贾,可吹牛的內(nèi)容都是我干的币喧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼袱耽,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼朱巨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起琼讽,我...
    開封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤钻蹬,失蹤者是張志新(化名)和其女友劉穎脉让,沒想到半個(gè)月后功炮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體术唬,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嫁怀,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年塘淑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蚂斤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡岗钩,死狀恐怖兼吓,靈堂內(nèi)的尸體忽然破棺而出森枪,到底是詐尸還是另有隱情县袱,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布棚愤,位于F島的核電站宛畦,受9級(jí)特大地震影響次和,放射性物質(zhì)發(fā)生泄漏那伐。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望诉探。 院中可真熱鬧肾胯,春花似錦、人聲如沸毕荐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽斋日。三九已至恶守,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間庸毫,已是汗流浹背飒赃。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工载佳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蔫慧,地道東北人姑躲。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓黍析,卻偏偏與公主長(zhǎng)得像橄仍,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子虑粥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容