[英]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.