繁体   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