-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDKHandleScroll.m
executable file
·177 lines (142 loc) · 5.19 KB
/
DKHandleScroll.m
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// HandleScroll.m
//
//
// Created by Darshan Karekar on 17/01/17.
// Copyright © 2017 Darshan. All rights reserved.
//
#import "DKHandleScroll.h"
@implementation DKHandleScroll
static DKHandleScroll* _handleScroll = nil;
+(DKHandleScroll*) sharedInstance{
@synchronized ([DKHandleScroll class]) {
if (!_handleScroll)
{
DKHandleScroll *k = [[self alloc] init];
// NSLog(@"%@",k.description);
}
return _handleScroll;
}
return nil;
}
+(instancetype)alloc{
@synchronized ([DKHandleScroll class]) {
_handleScroll = [super alloc];
return _handleScroll;
}
return nil;
}
-(instancetype)init{
self = [super init];
if (self != nil){
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
_rootVC = appDelegate.window.rootViewController;
}
return self;
}
#pragma mark - ScrollView Delegates
- (void)keyboardWasShown:(NSNotification*)notification :(UIScrollView *)mainScroll :(UIViewController *)VC :(CGRect )TXFRAME
{
_reset = [mainScroll contentOffset];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight ) {
CGSize origKeySize = keyboardSize;
keyboardSize.height = origKeySize.width;
keyboardSize.width = origKeySize.height;
}
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
//NSLog(@"hei %f wi %f",kbSize.height,kbSize.width);
CGSize deviceSize=[UIScreen mainScreen].bounds.size;
float keyBdHeight;
if (kbSize.height<kbSize.width)
{
keyBdHeight=kbSize.height;
}
else
{
keyBdHeight=kbSize.width;
}
UIEdgeInsets contentInsets = UIEdgeInsetsMake(00.0, 0.0,keyBdHeight, 0.0);
mainScroll.contentInset = contentInsets;
mainScroll.scrollIndicatorInsets = contentInsets;
CGRect aRect = VC.view.frame;
aRect.size.height -= keyBdHeight;
TXFRAME.origin.y = TXFRAME.origin.y+TXFRAME.size.height+20;
if (!CGRectContainsPoint(aRect, TXFRAME.origin) ) {
CGPoint scrollPoint =CGPointMake(0.0,ABS(TXFRAME.origin.y+20-(deviceSize.height-keyBdHeight+22)));
[mainScroll setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification :(UIScrollView *)mainScroll
{
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0 ,0, 0, 0);
mainScroll.contentInset = contentInsets;
mainScroll.scrollIndicatorInsets = contentInsets;
[mainScroll setContentOffset:_reset animated:YES];
}
//method to move the view up/down whenever the keyboard is shown/dismissed
- (CGRect) convertView:(UIView*)view
{
CGRect rect = view.frame;
while(view.superview)
{
view = view.superview;
rect.origin.x += view.frame.origin.x;
rect.origin.y += view.frame.origin.y;
}
return rect;
}
-(void)registerForNotification :(UIViewController *)currentVC
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWasShown:(NSNotification *)notification
{
id k = [UIResponder currentFirstResponder];
if(([k isKindOfClass:[UITextField class]])||([k isKindOfClass:[UITextView class]]))
{
UIView *textview = (UIView *)k;
CGRect TXFRAME = [self convertView:textview];
[self keyboardWasShown:notification :_scrollView :_currentViewController :TXFRAME];
}
}
-(void)keyboardWillBeHidden:(NSNotification *)notification
{
[self keyboardWillBeHidden:notification :_scrollView];
}
-(void)makeScrollable:(UIScrollView *)scrollView
{
[self setCurrentViewController:[self findTopVC]];
[self setScrollView:scrollView];
[self registerForNotification:_currentViewController];
}
-(UIViewController *)findTopVC
{
UIViewController *top = nil;
if(_rootVC.navigationController !=nil)
{
top = _rootVC.navigationController.topViewController;
}
else if(_rootVC.tabBarController != nil){
top = _rootVC.tabBarController.selectedViewController;
}
else{
UIViewController *parentViewController = _rootVC;
while (parentViewController.presentedViewController != nil){
parentViewController = parentViewController.presentedViewController;
}
top = parentViewController;
}
// NSLog(@"Curent Top VC: %@", top.description);
return top;
}
@end