ios 如何修复“UICollectionViewFlowLayout未定义”当键盘来来去去

pxyaymoc  于 2023-06-25  发布在  iOS
关注(0)|答案(1)|浏览(111)

完整的信息是:The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section ...
在可能的情况下,水平集合视图填充视图的底部。当键盘下降时,我会收到上面的一系列消息。我观察到集合视图的contentSize变成了一个非常小的数字。但我没有试过任何办法让它停止抱怨:当contentSize更改时,多次尝试调整委托方法以返回较小的大小。

bxpogfeg

bxpogfeg1#

最终起作用的是,从键盘开始移动直到它完全隐藏的时候,抑制使布局无效的消息。目标应用程序是Objective-C,但转换为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

两个invalidate消息都发送到此对象。

相关问题