-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTextField.m
90 lines (77 loc) · 2.27 KB
/
AutoTextField.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
//
// AutoTextField.m
//
//
// Created by 马 瑞鹏 on 14-6-22.
// Copyright (c) 2014年 xuetangX. All rights reserved.
//
#import "AutoTextField.h"
#import "UIView+move.h"
#define XT_KEY_WINDOW [UIApplication sharedApplication].keyWindow
#define MAGIN_TO_KEYBOARD 20
@implementation AutoTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
- (void)setMasterView:(UIView *)masterView
{
if (_masterView) {
_masterView = nil;
}
_masterView = masterView;
self.originMasterFrame = _masterView.frame;
}
- (void)awakeFromNib
{
[self setup];
}
- (void)setup
{
[self registerNotifications];
}
- (void)dealloc
{
[self removeNotifications];
}
#pragma mark - 键盘消息
- (void)registerNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoradWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoradWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoradFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)removeNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyBoradFrameWillChange:(NSNotification *)noti
{
NSDictionary *userInfo = noti.userInfo;
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
// keyboardRect = [_masterView convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect selfFrameInWindow = [self.superview convertRect:self.frame toView:XT_KEY_WINDOW];
int offset = keyboardTop - (selfFrameInWindow.origin.y + selfFrameInWindow.size.height) - MAGIN_TO_KEYBOARD;
if (offset < 0) {
[_masterView verticalMove:offset animate:YES];
}
}
- (void)keyBoradWillShow:(NSNotification *)noti
{
}
- (void)keyBoradWillHide:(NSNotification *)noti
{
[self moveMasterViewToOrigin];
}
- (void)moveMasterViewToOrigin
{
[_masterView moveTo:_originMasterFrame.origin animate:YES];
}
@end