首页 > 移动平台 > 详细

UITextField跟随键盘移动

时间:2015-05-25 16:36:07      阅读:142      评论:0      收藏:0      [点我收藏+]

利用通知监测键盘的移动,从而改变输入框的位置

-(void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

}


- (void)viewDidLoad

{

[super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}


#pragma mark - UIKeyboardNotification

- (void)keyboardWillChangeFrame:(NSNotification *)notification

{

    NSDictionary *userInfo = notification.userInfo;

    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    

    void(^animations)() = ^{

        [self willShowKeyboardFromFrame:beginFrame toFrame:endFrame];

    };

    

    void(^completion)(BOOL) = ^(BOOL finished){

    };

    

    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:completion];

}


- (void)willShowKeyboardFromFrame:(CGRect)beginFrame toFrame:(CGRect)toFrame

{

    if (beginFrame.origin.y == [[UIScreen mainScreen] bounds].size.height){//将要移动的位置

        [UIView animateWithDuration:0.3 animations:^{

            _atextField.frame = RECTMAKE(0, CGRectGetHeight([self superview].frame)-CGRectGetHeight(self.frame)-toFrame.size.height+10, 320, 243);

        } completion:^(BOOL finished) {

        }];

    }else if (toFrame.origin.y == [[UIScreen mainScreen] bounds].size.height){//初始位置

        [UIView animateWithDuration:0.3 animations:^{

            _atextField.frame.frame = RECTMAKE(0, CGRectGetHeight([self superview].frame)-CGRectGetHeight(self.frame), 320, 243);

        }completion:^(BOOL finished) {

        }];

    }else{//将要移动的位置

        [UIView animateWithDuration:0.3 animations:^{

                _atextField.frame.frame = RECTMAKE(0, CGRectGetHeight([self superview].frame)-CGRectGetHeight(self.frame)-toFrame.size.height+10, 320, 243);

            } completion:^(BOOL finished) {

            }];

    }

}


UITextField跟随键盘移动

原文:http://blog.csdn.net/u012460084/article/details/45970685

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!