forked from greatyingzi/MTerminal-Jailed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MTKBAvoiding.m
57 lines (56 loc) · 1.76 KB
/
MTKBAvoiding.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
#include "MTKBAvoiding.h"
@implementation MTKBAvoiding
-(void)screenSizeDidChange {
// default implementation does nothing
}
-(void)adjustContentInset {
UIScrollView* view=(UIScrollView*)self.view;
UIEdgeInsets inset=view.contentInset;
if(inset.bottom!=kbHeight){
inset.bottom=kbHeight - view.safeAreaInsets.bottom;
view.contentInset=view.scrollIndicatorInsets=inset;
[self screenSizeDidChange];
}
}
-(void)viewSafeAreaInsetsDidChange {
[super viewSafeAreaInsetsDidChange];
[self adjustContentInset];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(deferAdjust){
deferAdjust=NO;
[self adjustContentInset];
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
deferAdjust=YES;
}
-(void)viewDidLoad {
NSNotificationCenter* center=[NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardWillChange:)
name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardWillChange:)
name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWillChange:(NSNotification*)note {
if([note.name isEqualToString:UIKeyboardWillShowNotification]){
CGRect frame=[self.view convertRect:[[note.userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
kbHeight=frame.size.height;
}
else {kbHeight=0;}
if(!deferAdjust){[self adjustContentInset];}
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return YES;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)orientation {
if(!kbHeight){[self screenSizeDidChange];}
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end