1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// TPKeyboardAvoidingCollectionView.m
// TPKeyboardAvoiding
//
// Created by Michael Tyson on 30/09/2013.
// Copyright 2015 A Tasty Pixel & The CocoaBots. All rights reserved.
//
#import "TPKeyboardAvoidingCollectionView.h"
@interface TPKeyboardAvoidingCollectionView () <UITextFieldDelegate, UITextViewDelegate>
@end
@implementation TPKeyboardAvoidingCollectionView
#pragma mark - Setup/Teardown
- (void)setup {
if ( [self hasAutomaticKeyboardAvoidingBehaviour] ) return;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToActiveTextField) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToActiveTextField) name:UITextFieldTextDidBeginEditingNotification object:nil];
}
-(id)initWithFrame:(CGRect)frame {
if ( !(self = [super initWithFrame:frame]) ) return nil;
[self setup];
return self;
}
- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
if ( !(self = [super initWithFrame:frame collectionViewLayout:layout]) ) return nil;
[self setup];
return self;
}
-(void)awakeFromNib {
[super awakeFromNib];
[self setup];
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
#if !__has_feature(objc_arc)
[super dealloc];
#endif
}
-(BOOL)hasAutomaticKeyboardAvoidingBehaviour {
if ( [[[UIDevice currentDevice] systemVersion] integerValue] >= 9
&& [self.delegate isKindOfClass:[UICollectionViewController class]] ) {
// Theory: It looks like iOS 9's collection views automatically avoid the keyboard. As usual
// Apple have totally failed to document this anywhere, so this is just a guess.
return YES;
}
return NO;
}
-(void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self TPKeyboardAvoiding_updateContentInset];
}
-(void)setContentSize:(CGSize)contentSize {
if (CGSizeEqualToSize(contentSize, self.contentSize)) {
// Prevent triggering contentSize when it's already the same that
// cause weird infinte scrolling and locking bug
return;
}
[super setContentSize:contentSize];
[self TPKeyboardAvoiding_updateContentInset];
}
- (BOOL)focusNextTextField {
return [self TPKeyboardAvoiding_focusNextTextField];
}
- (void)scrollToActiveTextField {
return [self TPKeyboardAvoiding_scrollToActiveTextField];
}
#pragma mark - Responders, events
-(void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if ( !newSuperview ) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) object:self];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self TPKeyboardAvoiding_findFirstResponderBeneathView:self] resignFirstResponder];
[super touchesEnded:touches withEvent:event];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( ![self focusNextTextField] ) {
[textField resignFirstResponder];
}
return YES;
}
-(void)layoutSubviews {
[super layoutSubviews];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) object:self];
[self performSelector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) withObject:self afterDelay:0.1];
}
@end