Programming

키보드 높이에 따른 뷰 수정

 2016. 12. 27. 11:37
반응형
1
2
@property CGFloat currentKeyboardHeight;
 
cs



1
2
3
4
5
6
7
8
- (void)viewDidAppear:(BOOL)animated{
    [self addKeyboardObserver];
}
 
- (void)viewDidDisappear:(BOOL)animated{
    [self removeKeyboardObserver];
}
 
cs



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
-(void) addKeyboardObserver{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
 
-(void) removeKeyboardObserver{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
}
 
- (void)keyboardWillShow:(NSNotification*)notification {
    
    NSLog(@"keyboardWillShow");
    
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    CGFloat deltaHeight = kbSize.height - _currentKeyboardHeight;
    // Write code to adjust views accordingly using deltaHeight
    _currentKeyboardHeight = kbSize.height;
    
    CGRect viewRect = self.view.frame;
    viewRect.size.height -= deltaHeight;
    self.view.frame = viewRect;
}
 
- (void)keyboardWillHide:(NSNotification*)notification{
    
    NSLog(@"keyboardWillHide");
    
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    // Write code to adjust views accordingly using kbSize.height
    _currentKeyboardHeight = 0.0f;
    
    CGRect viewRect = self.view.frame;
    viewRect.size.height += kbSize.height;
    self.view.frame = viewRect;
}
cs


반응형