由于似乎没有答案,其他人也遇到了这个问题,所以这里要求的是我目前的解决方案。
所以在为我添加了一个观察者之后,contentInset
我有了以下功能。
static bool _willSystemUpdateCollectionViewInset = NO;
static bool _willCustomKeyboardViewUpdateCollectionViewInset = NO;
static bool _didCancelDismissInteraction = NO;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(contentInset))])
{
id oldNumber = change[@"old"];
id newNumber = change[@"new"];
if (_willSystemUpdateCollectionViewInset)
{
_willSystemUpdateCollectionViewInset = NO;
UICollectionView *collectionView = (UICollectionView*)object;
UIEdgeInsets insets = collectionView.contentInset;
insets.bottom = [oldNumber UIEdgeInsetsValue].bottom;
[collectionView setContentInset:insets];
}
else if (_willCustomKeyboardViewUpdateCollectionViewInset)
{
_willCustomKeyboardViewUpdateCollectionViewInset = NO;
[self updateScrollViewInsets];
}
if ([newNumber UIEdgeInsetsValue].bottom > [oldNumber UIEdgeInsetsValue].bottom )
[_messageViewController scrollCollectionViewToBottom:NO];
}
else
{
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
因此,在键盘上显示或隐藏标志_willSystemUpdateCollectionViewInset
设置为 YES,上述功能实质上否定了系统自动进行的更改。