Nie działa układ automatyczny w UICollectionViewCell

Mam prosty UICollectionView z komórkami, które mają pojedynczy UITextView. UITextView jest ograniczony do krawędzi komórki, więc powinny one pozostać tej samej wielkości co rozmiar komórki.

Problem, który mam polega na tym, że z jakiegoś powodu te ograniczenia nie działają, gdy określam rozmiar komórki za pomocą collectionView:layout: sizeForItemAtIndexPath:.

Mam rozmiar komórki ustawiony na 320x50 w Storyboardzie. Jeśli zwrócę rozmiar z 2 razy wysokość rozmiaru komórki z sizeForItemAtIndexPath:, UITextView pozostaje na tej samej wysokości pomimo ograniczeń, które ustawiłem. Używam Xcode 6 GM.

Mój kod kontrolera widoku to:

@implementation TestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UICollectionViewCell *c = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];

    NSLog(@"%f", c.frame.size.height);

    UITextView *tv = (UITextView *)[c viewWithTag:9];
    NSLog(@"%f", tv.frame.size.height);

}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;

    CGSize size = flowLayout.itemSize;

    size.height = size.height * 2;

    return size;
}

@end

Te logi w viewDidAppear: daj mi wyjście z:
100.00000
50.00000
Jak widać, wysokość UITextView nie zmienia się wraz z wysokością komórki.


Oto zrzut ekranu storyboardu, który skonfigurowałem z UITextView ograniczonym w UICollectionViewCell:

Tutaj wpisz opis obrazka

Wiem używanie ograniczeń automatycznego układu działa dobrze z Uitableviewcell i dynamiczną zmianą rozmiaru. Nie mam pojęcia, dlaczego to nie działa w tej sprawie. Czy ktoś ma jakieś pomysły?

Author: ryanthon, 2014-09-12

4 answers

Cóż, właśnie zajrzałem na fora programistów iOS. Najwyraźniej jest to błąd z zestawem SDK iOS 8 działającym na urządzeniach z systemem iOS 7. Obejście polega na dodaniu następujących elementów do podklasy UICollectionViewCell:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
 109
Author: ryanthon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-13 05:38:52

Równoważny Kod Swift:

override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}
 40
Author: Hamza Ghazouani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-21 14:22:37

Oto rozwiązanie, jeśli nie podklasujesz UICollectionViewCell. Po prostu dodaj te dwie linie pod cellForItemAtIndexPath: Po dequeueReusableCellWithReuseIdentifier:

Obj-C

[[cell contentView] setFrame:[cell bounds]];
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

Swift - 2.0

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
 24
Author: itsji10dra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-04 10:01:02

Swift 2.0:

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
 4
Author: Bill Chan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-25 21:05:36