iOS相应键盘高度变化,相应控件随之变化的代码段

来源:互联网 发布:老公是唇膏男 知乎 编辑:程序博客网 时间:2024/05/21 11:22

//注册通知

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];


//响应通知

#pragma mark keyboard notification


- (void)keyboardWillShow:(NSNotification *) notification {

    float animationDuration = [[[notificationuserInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

    CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height;

    

    CGRect bottomBarFrame = self.mToolBar.frame;

    {

        [UIViewbeginAnimations:@"bottomBarUp"context:nil];

        [UIView setAnimationDuration: animationDuration];

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

        bottomBarFrame.origin.y =self.view.bounds.size.height -44 - height;

        self.mToolBar.frame = bottomBarFrame;

        [UIViewcommitAnimations];

    }

}


- (void)keyboardWillHide:(NSNotification *) notification {

    float animationDuration = [[[notificationuserInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

    CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height;

    

    CGRect bottomBarFrame = self.mToolBar.frame;

    if (bottomBarFrame.origin.y <300)

    {

        [UIViewbeginAnimations:@"bottomBarDown"context:nil];

        [UIView setAnimationDuration: animationDuration];

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

        bottomBarFrame.origin.y += height;

        self.mToolBar.frame = bottomBarFrame;

        [UIViewcommitAnimations];

    }

}

原创粉丝点击