UICollectionView不会在屏幕旋转时更新其布局

时间:2018-02-15 19:41:35

标签: ios objective-c uiviewcontroller uicollectionview uicollectionviewcell

我有一个UIView的自定义UICollectionView

在屏幕旋转时,我试图让UICollectionView在屏幕上伸展,然后重绘其单元格。

我下载了数据后,我尝试了[grid setNeedsLayout][grid setNeedsDisplay],但这并没有用。

这就是我想要发生的事情:

人像

enter image description here

风景

(这也是应用程序在横向启动时的显示方式,但如果您更改为纵向,则会更新。)

enter image description here

但如果我以Portrait模式开始并切换到Landscape,这就是我得到的。

enter image description here

我正在以编程方式创建这些视图。我没有使用任何故事板。

我试过了:

-(void)viewDidLayoutSubviews {
    grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];

}

我也尝试过玩弄:

- (void) viewWillLayoutSubviews {

    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        //LANDSCAPE
        if(grid){
            NSLog(@"Grid Needs Landscape Layout");
            grid.frame =CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
            [grid refreshData];
        }

    }else {
        //PORTRIAT
        if(grid){
            NSLog(@"Grid Needs Portrait Layout");
            grid.frame =CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
            [grid refreshData];
        }
    }
}

但我无法伸展。

有任何帮助吗?

MyThumbnailGridView

@interface ViewController () <UINavigationControllerDelegate> {

    MyThumbnailGridView *grid;

    NSMutableArray * arrImages;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    arrImages = [NSMutableArray new];

    grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
    //grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectZero];
    NSLog(@"showThumbnailGrid Grid View Size: %@", NSStringFromCGRect(grid.frame));

    [self.view addSubview:grid];

    [self getListOfImages];
}


-(void) getListOfImages {
        //Do background task to get images  and fill arrImages
         [self onDownloadImageDataComplete];    
}


- (void) onDownloadImageDataComplete{
    grid.imageDataSource = arrImages;
    //  [grid setNeedsLayout];
    //  [grid setNeedsDisplay];
}

//...
@end

* MyThumbnailGridView.h

@interface MyThumbnailGridView : UIView

-(id)initWithFrame:(CGRect)frame;
-(void) refreshData;

@property (nonatomic,strong) NSArray *imageDataSource;

@end

* MyThumbnailGridView.m

@interface MyThumbnailGridView () <UICollectionViewDelegate, UICollectionViewDataSource>{
    UICollectionView *collectionView;
}

@end

@implementation MyThumbnailGridView


- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        [self customInit];

    }
    return self;
}
- (void) customInit {
    collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[[MyFlowLayout alloc] init]];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.allowsMultipleSelection = NO;
    collectionView.showsVerticalScrollIndicator = YES;
    [collectionView setBackgroundColor:[UIColor darkGrayColor]];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyId"];
    [self addSubview:collectionView];
}
- (void) refreshData {
    NSLog(@"Refresh Grid Data");
    [collectionView reloadData];
}

////other code
@end

MyFlowLayout

@interface MyFlowLayout : UICollectionViewFlowLayout
@end

@implementation MyFlowLayout
- (instancetype)init{
    self = [super init];
    if (self)
    {
        self.minimumLineSpacing = 1.0;
        self.minimumInteritemSpacing = 1.0;
        self.scrollDirection = UICollectionViewScrollDirectionVertical;

    }
    return self;
}

- (CGSize)itemSize {
    NSInteger numberOfColumns = 3;
    CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
    return CGSizeMake(itemWidth, itemWidth);
}

@end

MyCollectionViewCell

@interface MyCollectionViewCell : UICollectionViewCell
@property (strong, nonatomic)  UIImageView *imageView;
@end
@implementation MyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.imageView = [UIImageView new];
        [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
        [self.imageView setClipsToBounds:YES];
        [self.imageView setBackgroundColor:[UIColor darkGrayColor]];
        [self.contentView addSubview:self.imageView];

    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.imageView.image = nil;
    [self.imageView setHidden:NO];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.imageView setFrame:self.contentView.bounds];
}
@end

2 个答案:

答案 0 :(得分:1)

这可以解决 您可以在方向的委托函数中更改MyThumbnailGridView视图的框架,也可以使用此类约束创建视图

(void)viewDidLoad {

   [super viewDidLoad];

    arrImages = [NSMutableArray new];

    grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];

    [self.view addSubview:grid];    

    [self getListOfImages];

 }

-(void)viewDidLayoutSubviews
{  
   if(Once){

        Once = NO;

       // adding constraints

  MyThumbnailGridView.translatesAutoresizingMaskIntoConstraints = NO;

  [self.MyThumbnailGridView.widthAnchor constraintEqualToConstant:self.view.frame.size.width].active = YES;

  [self.MyThumbnailGridView.heightAnchor constraintEqualToConstant:self.view.frame.size.height/2].active = YES;

  [self.MyThumbnailGridView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;

  [self.MyThumbnailGridView.topAnchor  constraintEqualToAnchor:self.view.topAnchor constant:self.view.frame.size.height/2].active = YES;



    }
}

同时实现此

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [self.view layoutIfNeeded];
    [MyThumbnailGridView.collectionView invalidate];
// Do view manipulation here.
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

答案 1 :(得分:0)

问题是当应用程序旋转时,集合视图本身不会改变大小。您已将集合视图指定为固定frame,然后就走开了。所以它永远不会改变。所以永远不要再自我解决了。

你应该将你的MyThumbnailGridView及其集合视图子视图自动布局约束提供给他们的超级视图,以便他们在应用程序旋转时正确地改变大小。