-
Notifications
You must be signed in to change notification settings - Fork 3
/
FUNTableView.m
149 lines (125 loc) · 5.73 KB
/
FUNTableView.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
//
// FUNTableView.m
// perimeter
//
// Created by M. Robert Spryn on 7/22/11.
// Copyright 2011 PowerPro. All rights reserved.
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "FUNTableView.h"
CGRect XYWidthHeightRectSwap(CGRect rect);
CGRect FixOriginRotation(CGRect rect, UIInterfaceOrientation orientation, int parentWidth, int parentHeight);
@interface FUNTableView()
@end
@implementation FUNTableView
@synthesize topMostRowBeforeKeyBoardWasShown, originalFrame;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc
{
self.topMostRowBeforeKeyBoardWasShown = nil;
[super dealloc];
}
- (void)adjustTableViewForKeyboardwithCell:(UITableViewCell *)cell userInformation:(NSDictionary *)userInfo
{
if ([self indexPathsForVisibleRows].count) {
NSIndexPath *indexPath = (NSIndexPath*)[[self indexPathsForVisibleRows] objectAtIndex:0];
self.topMostRowBeforeKeyBoardWasShown = indexPath;
}
NSValue* keyboardFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
id *appDelegate = [[UIApplication sharedApplication] delegate];
// Reduce the tableView height by the part of the keyboard that actually covers the tableView
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect windowRect = appDelegate.window.bounds;
CGRect viewRectAbsolute = [self convertRect:self.bounds toView:nil];
CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) {
windowRect = XYWidthHeightRectSwap(windowRect);
viewRectAbsolute = XYWidthHeightRectSwap(viewRectAbsolute);
keyboardFrame = XYWidthHeightRectSwap(keyboardFrame);
}
// fix the coordinates of our rect to have a top left origin 0,0
viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height);
CGRect frame = self.frame;
// store the value on the table for later
self.originalFrame = frame;
int remainder = (viewRectAbsolute.origin.y + viewRectAbsolute.size.height + keyboardFrame.size.height) - windowRect.size.height;
if (remainder > 0 && !(remainder > frame.size.height + 50)) {
frame.size.height = frame.size.height - remainder;
float duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration: duration
animations:^{
self.frame = frame;
}
completion:^(BOOL finished){
if (cell) {
NSIndexPath *textFieldIndexPath = [self indexPathForCell:cell];
[self scrollToRowAtIndexPath:textFieldIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}];
}
}
- (void)adjustTableViewNoKeyboardWithUserInformation:(NSDictionary *)userInfo scrollToOriginalCell:(BOOL)shouldScroll
{
float duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// update the frame with the old size
CGRect newFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.originalFrame.size.width, self.originalFrame.size.height);
[UIView animateWithDuration: duration
delay: 0.0
options: (UIViewAnimationOptionBeginFromCurrentState)
animations:^{
self.frame = newFrame;
}
completion:^(BOOL finished){
if (self.topMostRowBeforeKeyBoardWasShown && shouldScroll) {
[self scrollToRowAtIndexPath:self.topMostRowBeforeKeyBoardWasShown atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}];
}
#pragma mark CGRect Utility functions
CGRect XYWidthHeightRectSwap(CGRect rect) {
CGRect newRect;
newRect.origin.x = rect.origin.y;
newRect.origin.y = rect.origin.x;
newRect.size.width = rect.size.height;
newRect.size.height = rect.size.width;
return newRect;
}
CGRect FixOriginRotation(CGRect rect, UIInterfaceOrientation orientation, int parentWidth, int parentHeight) {
CGRect newRect;
switch(orientation)
{
case UIInterfaceOrientationLandscapeLeft:
newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), rect.origin.y, rect.size.width, rect.size.height);
break;
case UIInterfaceOrientationLandscapeRight:
newRect = CGRectMake(rect.origin.x, parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
break;
case UIInterfaceOrientationPortrait:
newRect = rect;
break;
case UIInterfaceOrientationPortraitUpsideDown:
newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
break;
}
return newRect;
}
@end