0

The full message is: The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section ...

In may situation, a horizontal collection view fill the bottom of the view. When the keyboard goes down, I get a slew of the above messages. I observed that the contentSize of the collection view changed to some really small number. But nothing I tried would get it to stop complaining: several attempts to adjust the delegate method to return a smaller size when the contentSize changed.

David H
  • 40,852
  • 12
  • 92
  • 138

1 Answers1

0

What finally worked was to suppress the messages that invalidate the layout from the time the keyboard starts moving until it's completely hidden. The target app is Objective-C but it will be trivial to convert to Swift.

@interface SSCollectionViewFlowLayout : UICollectionViewFlowLayout
@property (nonatomic, assign) BOOL shouldSuppress;
@end

@implementation SSCollectionViewFlowLayout
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardUp)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDown)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    return self;
}

- (void)keyboardUp {
    self.shouldSuppress = YES;
}

- (void)keyboardDown {
    self.shouldSuppress = NO;
}

- (void)prepareLayout {
    if(self.shouldSuppress) { return; }
    [super prepareLayout];
}

- (void)invalidateLayout {
    if(self.shouldSuppress) { return; }
    [super invalidateLayout];
}

- (void)invalidateLayoutWithContext:(UICollectionViewLayoutInvalidationContext *)context {
    if(self.shouldSuppress) { return; }
    [super invalidateLayoutWithContext:context];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end

Both the invalidate messages were sent to this object.

David H
  • 40,852
  • 12
  • 92
  • 138