框架类的 键盘监控 消退

来源:互联网 发布:raphael js api 编辑:程序博客网 时间:2024/04/29 04:25
- (void)viewDidLoad
{
    [superviewDidLoad];
 
    // register for keyboard notifications
    [[NSNotificationCenterdefaultCenter]addObserver:self
       selector:@selector(keyboardWillShow:)
       name:UIKeyboardWillShowNotification
       object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenterdefaultCenter]addObserver:self
       selector:@selector(keyboardWillHide:)
       name:UIKeyboardWillHideNotification
       object:self.view.window];
    keyboardIsShown = NO;
    //make contentSize bigger than your scrollSize
    //(you will need to figure out for your own use case)
    CGSizescrollContentSize = CGSizeMake(320,345);
    self.scrollView.contentSize= scrollContentSize;
}
 
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
 
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenterdefaultCenter]removeObserver:self
       name:UIKeyboardWillShowNotification
       object:nil];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenterdefaultCenter]removeObserver:self
       name:UIKeyboardWillHideNotification
       object:nil]; 
}
 
- (void)dealloc {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
 
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenterdefaultCenter]removeObserver:self
       name:UIKeyboardWillShowNotification
       object:nil];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenterdefaultCenter]removeObserver:self
       name:UIKeyboardWillHideNotification
       object:nil]; 
}
 
- (void)keyboardWillHide:(NSNotification*)n
{
    NSDictionary* userInfo = [n userInfo];
    // get the size of the keyboard
    CGSizekeyboardSize =
        [[userInfoobjectForKey:UIKeyboardFrameBeginUserInfoKey]
           CGRectValue].size;
    // resize the scrollview
    CGRectviewFrame = self.scrollView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView
    // was offset by the UITabBar so really only the portion of the keyboard that
    // is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height+= (keyboardSize.height- kTabBarHeight);
    [UIViewbeginAnimations:nilcontext:NULL];
    [UIViewsetAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIViewsetAnimationDuration:kKeyboardAnimationDuration];
    [self.scrollViewsetFrame:viewFrame];
    [UIViewcommitAnimations];
    keyboardIsShown = NO;
}
 
- (void)keyboardWillShow:(NSNotification*)n
{
    // This is an ivar I'm using to ensure that we do not do the frame size
    // adjustment on the UIScrollView if the keyboard is already shown. 
    // This can happen if the user, after fixing editing a UITextField,
    // scrolls the resized UIScrollView to another UITextField and attempts
    // to edit the next UITextField.  If we were to resize the UIScrollView again,
    // it would be disastrous. NOTE: The keyboard notification will fire
    // even when the keyboard is already shown.
    if(keyboardIsShown) {
        return;
    }
 
    NSDictionary* userInfo = [n userInfo];
 
    // get the size of the keyboard
    CGSizekeyboardSize =
        [[userInfoobjectForKey:UIKeyboardFrameBeginUserInfoKey]
           CGRectValue].size;
 
    // resize the noteView
    CGRectviewFrame = self.scrollView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView
    // was offset by the UITabBar so really only the portion of the keyboard
    // that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height-= (keyboardSize.height- kTabBarHeight);
    [UIViewbeginAnimations:nilcontext:NULL];
    [UIViewsetAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIViewsetAnimationDuration:kKeyboardAnimationDuration];
    [self.scrollViewsetFrame:viewFrame];
    [UIViewcommitAnimations];
    keyboardIsShown = YES;
}
Tag : keyboard , UIScrollView , 键盘