随着iOS 9的最新发行,可能需要对现有代码进行一些更新,以补偿对apples API所做的任何更改。最近看来,他们已经做到了,这样当键盘出现时,收藏夹视图现在可以自动调整其内容插入。这对于不手动处理或不支持多个操作系统版本的人很有用。在我的应用程序中,这有点令人头疼。我终于想出了一个使用KVO的解决方案,可以在系统更改插页时通知我,并且我做出了相应的反应,除单个边缘情况外,其他一切都工作正常。
如果我显示键盘,然后尝试通过交互式轻扫返回到导航堆栈中,从而导致调用beginAppearanceTransition:animated:
,但先取消它,然后点按键盘侧面以退出第一响应者,系统将突然决定它不想自动更新我的插图,并且内容触发器未触发我的KVO,键盘消失了,但内容插图没有减少,导致它看起来太错误了……如果我点击文本字段,再次显示键盘,突然决定再次进行自动更新。
有人对取消更新我的插图的交互式过渡后为什么忽略我的第一次键盘解雇有任何想法吗?
[必须重新审视此问题,因为团队认为它过于脆弱和hacky,并且在与this一起玩以找出他们如何处理同一案件后,他们似乎不必处理来自于哪里。因此,我将UICollectionView子类化,并覆盖setContentInset函数,仅在此处找到有问题的调用]
除了堆栈跟踪在这一点上不是特别有用,任何人有任何想法吗?
由于似乎没有答案,其他人也遇到了这个问题,这是我目前的解决方案。
因此,在为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,并且上述功能从本质上抵消了系统自动进行的更改。