簡體   English   中英

設備方向更改時如何重新排列集合視圖單元格

[英]How to rearrange collection view cell when device orientation changes

在此處輸入圖片說明

在縱向模式下,收藏夾視圖看起來很好。 但是,當采用橫向模式時,單元格不會重新排列..它們保留在同一位置。 在此處輸入圖片說明

我添加了自動布局,以便集合視圖可以拉伸以填充屏幕,並且集合視圖具有恆定的高度約束。

我的視圖層次是這樣的

視圖-> ScrollView-> ContentView |->有問題的集合視圖(通知標簽上方的一個)| |-> Tableview(在通知標簽之后一個) |->集合視圖(頂部黑色空間)

集合視圖的代碼是

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView==self.offerCollectionView) {
    sliderCollectionInHomeScreen *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseIdForSlider"
                                                                                     forIndexPath:indexPath];
    myCell.labelForShopName.text = [[self.arrayForOfferSlider objectAtIndex:indexPath.row] objectForKey:@"shop_name"];
    myCell.labelForOfferName.text = [[self.arrayForOfferSlider objectAtIndex:indexPath.row] objectForKey:@"offer_title"];
    myCell.offerImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.arrayForOfferSlider objectAtIndex:indexPath.row] objectForKey:@"img_url"]]]];

    ;


    return myCell;
}

我不知道這是否正確。 但這暫時對我有用

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrient{

//[self.categoryCollectionView reloadData];
UICollectionViewFlowLayout * lay =[[UICollectionViewFlowLayout alloc] init];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    [lay setScrollDirection:UICollectionViewScrollDirectionVertical];
}
else{

    [lay setScrollDirection:UICollectionViewScrollDirectionHorizontal];
}


[self.CollectionView setCollectionViewLayout:lay];


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM