KVO集合在iOS9中查看内容插入

Gen*_*ain 5 key-value-observing ios uiedgeinsets uicollectionview ios9

随着最近发布的iOS 9,可能需要对现有代码进行一些更新,以补偿对Apple API所做的任何更改.最近似乎他们已经做到了这样,当键盘出现时,集合视图现在会自动调整它的内容插入.这对于未手动处理或支持多个OS版本的用户非常有用.在我的应用程序中,它引起了一些头痛.我终于想出了一个使用KVO的解决方案来通知我系统何时更改了插件并且我做出了相应的反应,除了单个边缘情况外,一切正常.

如果我显示键盘,然后尝试通过交互式滑动返回导航堆栈导致beginAppearanceTransition:animated:被调用,但然后取消它然后点击键盘侧面以退出第一响应者,系统突然决定它做不想自动更新我的插件,我的KVO永远不会被内容插入触发,键盘消失但内容插入没有减少导致它看起来太错误...如果我点击文本字段导致键盘再次显示,突然它决定再次进行自动更新.

有没有人知道为什么它在取消更新我的插图的交互式转换后忽略了我对键盘的第一次解雇?

编辑

不得不重新审视这个问题,因为团队觉得它太脆弱和黑客,并且在玩这个以找出他们如何处理相同的情况后,他们似乎不必处理来自无处的错误呼叫.所以我子类UICollectionView并覆盖setContentInset函数只是为了在这里找到有问题的调用

IMG http://i63.tinypic.com/2hgqr7t.png

除了堆栈跟踪在这一点上没有特别的帮助,有没有人有任何想法?

Gen*_*ain 1

由于似乎没有答案,并且其他人也遇到了这个问题,因此这里要求的是我当前的解决方案。

因此,在添加观察者后,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];
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,在键盘上显示或隐藏标志_willSystemUpdateCollectionViewInset被设置为YES,并且上述函数本质上否定了系统自动所做的更改。