6

我正在尝试PullDown To Search在我的应用程序中实现功能。为了实现这一点,我刚刚修改了 EGOTableViewPullRefresh 位,除了一个问题外它运行良好。

问题

当用户打开应用程序时,将出现以下屏幕。最初UICollectionView's contentoffset应该是(0,0) 在此处输入图像描述

如果用户下拉集合视图,此时将出现contentoffset以下UICollectionView屏幕(0,-60) 在此处输入图像描述

用户可以通过在上面的屏幕中输入他们的文本进行搜索。一旦用户点击输入更改UITextField文本并且 UI 看起来像下面的屏幕contentoffset,我的问题就会出现在这个屏幕上。我不确定为什么会发生这种变化,你能指导我解决这个问题吗?UICollectionView(0,-60)(0,-110)

在此处输入图像描述

4

5 回答 5

2

有同样的问题。覆盖viewWillAppear:viewDidLoad:以及文档中所述的其他方法没有效果,TPKeyboardAvoidingScrollView也没有帮助。在与收藏视图苦苦挣扎 2 多天后,我遇到了一个非常糟糕的解决方法。这个想法是在键盘即将出现时锁定向上滚动,因此您的集合视图不会移动到任何地方,并在键盘结束动画后解锁滚动。为此,您应该子类化UICollectionView以添加锁定滚动的标志,订阅键盘通知并正确设置此标志。

在您实施此解决方法之前,我必须警告您这是一个非常糟糕的主意,您应该在这样做之前尝试其他所有方法。不过,这行得通...

所以这就是你要做的:

  1. 在 viewController 的 viewWillAppear 中订阅键盘通知:

    - (void)viewWillAppear:(BOOL)animated
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardDidShow:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        // you custom code here
    }
    

您将稍后处理通知

  1. 不要忘记取消订阅通知:

    - (void)viewWillDisappear:(BOOL)animated
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        // your custom code
    }
    
  2. 子类 UICollectionView 并添加标志:

    @property (nonatomic, getter=isLocked) BOOL locked;
    
  3. 覆盖 setContentOffset: 在您的集合视图中:

    - (void)setContentOffset:(CGPoint)contentOffset
    {
        if (contentOffset.y < self.contentOffset.y && self.isLocked) // user scrolls up
            contentOffset.y = self.contentOffset.y; // ignore new Y coordinate, so collection view will not scroll up
        [super setContentOffset:contentOffset];
    }
    
  4. 在您的 viewController 中创建用于键盘处理以锁定和解锁滚动的方法:

    - (void)keyboardWillShow:(NSNotification *)aNotification
    {
        [(LockableCollectionView *)self.collectionView setLocked:YES];
    }
    
    - (void)keyboardDidShow:(NSNotification *)aNotification
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [(LockableCollectionView *)self.collectionView setLocked:NO];
        });
    }
    

警告! 如果您在键盘显示后立即解锁滚动,这里有一点说明,锁定将被忽略并且集合视图将向上dispatch_after滚动。keyboardDidShow:将其包装成dispatch_after0.3 秒后运行此代码,它运行良好。这可能与运行循环有关,应该在真实设备上进行测试,而不是在模拟器中。

于 2015-04-02T18:22:28.483 回答
1

问题解决!通过 UIViewController 更改我的 UICollectionViewController 并使我的 UICollectionView 成为我的 ViewController.view 的子视图。

于 2017-06-20T14:13:48.570 回答
1

我在 UICollectionView 中有一个 UITableView 并得到了奇怪的滚动。受@AnkH 的启发,我重写了我的 UICollectionView 并将其添加到构造函数中:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow),
            name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide),
            name: NSNotification.Name.UIKeyboardDidHide, object: nil)

也覆盖 contentOffset 并在键盘可见时阻止对 contentOffset 的所有修改。完毕。问题是 UITableView 本身已经触发了正确的 contentOffset 设置,但是随后父 UICollectionView 添加了自己的 contentOffset,导致了灾难性的行为。首先它向上滚动太远,然后向下滚动太远。现在它完美无缺!

于 2018-01-18T10:26:52.303 回答
0

也不是一个很好的解决方案来处理这个

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)

 

func keyboardWillShow()  {
    let offset = self.collectionView!.contentOffset;
    self.collectionView!.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right:0 )
    self.collectionView!.contentOffset = offset

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) {
        let offset = self.collectionView!.contentOffset;
        self.collectionView!.contentInset = UIEdgeInsets(top: 60, left: 0, bottom: 0, right:0 )
        self.collectionView!.contentOffset = offset
    }
}
于 2016-05-10T19:26:17.327 回答
0

最简单的替代方法是使用 UIViewController 并添加您自己的 UICollectionView。禁用键盘所有不良行为所需的疯狂黑客数量简直是疯狂的。

于 2016-11-13T22:39:23.667 回答