Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions TPKeyboardAvoiding/TPKeyboardAvoidingAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// TPKeyboardAvoidingAction.h
// postepay-Simulatore
//
// Created by Antonio Scardigno on 10/01/2020.
// Copyright © 2020 Poste Italiane SPA. All rights reserved.
//

NS_ASSUME_NONNULL_BEGIN

typedef NSString *TPKeyboardAvoidingAction NS_TYPED_ENUM;
extern TPKeyboardAvoidingAction const TPKeyboardAvoidingActionNextTextField;
extern TPKeyboardAvoidingAction const TPKeyboardAvoidingActionPrevTextField;

NS_ASSUME_NONNULL_END
12 changes: 12 additions & 0 deletions TPKeyboardAvoiding/TPKeyboardAvoidingAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// TPKeyboardAvoidingAction.m
// postepay-Simulatore
//
// Created by Antonio Scardigno on 10/01/2020.
// Copyright © 2020 Poste Italiane SPA. All rights reserved.
//

#import "TPKeyboardAvoidingAction.h"

TPKeyboardAvoidingAction const TPKeyboardAvoidingActionNextTextField = @"TPKeyboardAvoidingActionNextTextField";
TPKeyboardAvoidingAction const TPKeyboardAvoidingActionPrevTextField = @"TPKeyboardAvoidingActionPrevTextField";
Empty file modified TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.h
100644 → 100755
Empty file.
64 changes: 60 additions & 4 deletions TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,73 @@ - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
}

-(void)layoutSubviews {
[super layoutSubviews];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) object:self];
[self performSelector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) withObject:self afterDelay:0.1];
}

#pragma mark - UITextField delegate methods

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( ![self focusNextTextField] ) {
[textField resignFirstResponder];
}

if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldReturn:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldReturn:textField];
}

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];
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if ([[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
[[[self textFieldDelegates] objectForKey:textField] textFieldDidBeginEditing:textField];
}
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
if ([[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldDidEndEditing:)]) {
[[[self textFieldDelegates] objectForKey:textField] textFieldDidEndEditing:textField];
}
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldBeginEditing:textField];
}

return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldEndEditing:textField];
}

return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textField:textField shouldChangeCharactersInRange:range replacementString:string];
}

return YES;
}

-(BOOL)textFieldShouldClear:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldClear:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldClear:textField];
}

return YES;
}

@end
64 changes: 60 additions & 4 deletions TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,73 @@ - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
}

-(void)layoutSubviews {
[super layoutSubviews];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) object:self];
[self performSelector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) withObject:self afterDelay:0.1];
}

#pragma mark - UITextField delegate methods

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( ![self focusNextTextField] ) {
[textField resignFirstResponder];
}

if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldReturn:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldReturn:textField];
}

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];
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if ([[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
[[[self textFieldDelegates] objectForKey:textField] textFieldDidBeginEditing:textField];
}
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
if ([[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldDidEndEditing:)]) {
[[[self textFieldDelegates] objectForKey:textField] textFieldDidEndEditing:textField];
}
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldBeginEditing:textField];
}

return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldEndEditing:textField];
}

return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textField:textField shouldChangeCharactersInRange:range replacementString:string];
}

return YES;
}

-(BOOL)textFieldShouldClear:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldClear:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldClear:textField];
}

return YES;
}

@end
Empty file modified TPKeyboardAvoiding/TPKeyboardAvoidingTableView.h
100644 → 100755
Empty file.
80 changes: 75 additions & 5 deletions TPKeyboardAvoiding/TPKeyboardAvoidingTableView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ - (void)setup {
[[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];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToNextTextField) name:TPKeyboardAvoidingActionNextTextField object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToPrevTextField) name:TPKeyboardAvoidingActionPrevTextField object:nil];
}

-(id)initWithFrame:(CGRect)frame {
Expand Down Expand Up @@ -81,14 +83,26 @@ -(void)setContentSize:(CGSize)contentSize {

- (BOOL)focusNextTextField {
return [self TPKeyboardAvoiding_focusNextTextField];

}

- (BOOL)focusPrevTextField {
return [self TPKeyboardAvoiding_focusPrevTextField];
}

- (void)scrollToActiveTextField {
return [self TPKeyboardAvoiding_scrollToActiveTextField];
}

#pragma mark - Responders, events

-(void) goToNextTextField {
[self focusNextTextField];
}

-(void) goToPrevTextField {
[self focusPrevTextField];
}

-(void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if ( !newSuperview ) {
Expand All @@ -101,17 +115,73 @@ - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
}

-(void)layoutSubviews {
[super layoutSubviews];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) object:self];
[self performSelector:@selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:) withObject:self afterDelay:0.1];
}

#pragma mark - UITextField delegate methods

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( ![self focusNextTextField] ) {
[textField resignFirstResponder];
}

if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldReturn:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldReturn:textField];
}

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];
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if ([[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
[[[self textFieldDelegates] objectForKey:textField] textFieldDidBeginEditing:textField];
}
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
if ([[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldDidEndEditing:)]) {
[[[self textFieldDelegates] objectForKey:textField] textFieldDidEndEditing:textField];
}
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldBeginEditing:textField];
}

return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldEndEditing:textField];
}

return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textField:textField shouldChangeCharactersInRange:range replacementString:string];
}

return YES;
}

-(BOOL)textFieldShouldClear:(UITextField *)textField {
if ([[self textFieldDelegates] objectForKey:textField]
&& [[[self textFieldDelegates] objectForKey:textField] respondsToSelector:@selector(textFieldShouldClear:)]) {
return [[[self textFieldDelegates] objectForKey:textField] textFieldShouldClear:textField];
}

return YES;
}

@end
7 changes: 6 additions & 1 deletion TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
//

#import <UIKit/UIKit.h>
#import "TPKeyboardAvoidingAction.h"

@interface UIScrollView (TPKeyboardAvoidingAdditions)

@property (strong, nonatomic) NSMapTable<UIView *, id<UITextFieldDelegate>>* textFieldDelegates;

- (BOOL)TPKeyboardAvoiding_focusNextTextField;
- (BOOL)TPKeyboardAvoiding_focusPrevTextField;
- (void)TPKeyboardAvoiding_scrollToActiveTextField;

- (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification;
Expand All @@ -18,5 +23,5 @@
- (void)TPKeyboardAvoiding_updateFromContentSizeChange;
- (void)TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:(UIView*)view;
- (UIView*)TPKeyboardAvoiding_findFirstResponderBeneathView:(UIView*)view;
-(CGSize)TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames;
- (CGSize)TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames;
@end
Loading