There was the same problem. Overriding viewWillAppear: viewDidLoad: and the other methods described in the documentation did not affect, TPKeyboardAvoidingScrollView did not help either. After I struggled with viewing the collection for more than 2 days, I came up with a very bad workaround. The idea is to block scrolling when the keyboard is about to appear, so your collection view won't move anywhere and unlocks scrolling after animating the end of the keyboard. To do this, you must subclass UICollectionView to add a flag that blocks scrolling, subscribes to keyboard notifications, and sets this flag correctly.
Before you start using this workaround, I must warn you that this is a VERY bad idea, and before that you should try everything else. However, this works ...
So here is what you do:
In the WillAppear view of your viewController, sign up for a notification keyboard:
- (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 will process notifications later.
Do not forget to unsubscribe from notifications:
- (void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self];
Subclass of UICollectionView and Add Flag:
@property (nonatomic, getter=isLocked) BOOL locked;
Override setContentOffset: in your collection view:
- (void)setContentOffset:(CGPoint)contentOffset { if (contentOffset.y < self.contentOffset.y && self.isLocked)
In your viewController, create keyboard handling methods to lock and unlock scrolling:
- (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]; }); }
A warning! Below is a little explanation about dispatch_after in keyboardDidShow: If you unlock the scroll immediately after the keyboard is displayed, the lock will be ignored and the collection will scroll up. Wrap it in dispatch_after to run this code after 0.3 seconds and it works well. This is probably due to run cycles and should be tested on real devices, not in the simulator.
source share