简体   繁体   English

设备方向更改时如何重新排列集合视图单元格

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

在此处输入图片说明

Collection view looks fine when in portrait mode. 在纵向模式下,收藏夹视图看起来很好。 But when it landscape mode the cells do not rearrange .. they remain in the same place. 但是,当采用横向模式时,单元格不会重新排列..它们保留在同一位置。 在此处输入图片说明

I have added autolayout so that the collection view stretches to fill the screen and the collection view has a constant height constraint. 我添加了自动布局,以便集合视图可以拉伸以填充屏幕,并且集合视图具有恒定的高度约束。

My view hierarchy is like this 我的视图层次是这样的

View ->ScrollView->ContentView |--> Problematic collection view (one above notifications label) | 视图-> ScrollView-> ContentView |->有问题的集合视图(通知标签上方的一个)| |-> Tableview (one after after the notifications label) | |-> Tableview(在通知标签之后一个) |->Collection view (black space on top) |->集合视图(顶部黑色空间)

The code for the collection view is 集合视图的代码是

-(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;
}

I don't know if this is correct . 我不知道这是否正确。 But this has worked for me for the time being 但这暂时对我有用

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

相关问题 长按模式下的收藏夹视图时,如何移动收藏夹视图单元格并在iOS中重新排列它们? - How to move collection view cell & rearrange them in iOS when collection view in long press mode? 设备方向更改时调整PDF查看大小 - Resize PDF View when device orientation changes 设备方向更改时更改视图控制器 - changing view controller when the device orientation changes 设备方向改变时以编程方式重绘视图的简单方法 - Simple way to redraw view programatically when device orientation changes 当设备方向迅速改变时,浮动操作按钮的视图不出现 - View of floating action button not appearing when device orientation changes in swift 添加新单元格后,集合视图单元格静态变量发生更改 - Collection View Cell static variable changes when new cell added 设备方向快速变化时如何旋转按钮? - How to rotate button when device orientation changes in swift? 查看控制器层次结构和设备方向更改 - view controller hierarchy and device orientation changes 当iPhone / iPad的设备方向改变时,如何处理UI组件? - How to handle UI components when device orientation for iPhone/iPad changes? 子视图方向更改时如何旋转父视图 - How to rotate parent view when child view orientation changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM