弹出键盘时控件位置变化

来源:互联网 发布:java 字符串pack方法 编辑:程序博客网 时间:2024/06/04 19:51

//获得键盘高度

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(onKeyBoardShow:)name:UIKeyboardWillShowNotificationobject:Nil];

    oldFrame=self.myButton.frame;


-(void)onKeyBoardShow:(id)sender{

    //获得键盘高度、坐标

    NSNotification *notice=sender;

    NSDictionary *dic=notice.userInfo;

    id keyBoardFrame=dic[UIKeyboardFrameEndUserInfoKey];

    CGRect keyBoardRect;

    [keyBoardFrame getValue:&keyBoardRect];

    //重新设置按键的坐标

    CGRect newRect=oldFrame;

    newRect.origin.y-=keyBoardRect.size.height;

    [UIView animateWithDuration:0.3animations:^{

        self.myButton.frame=newRect;

    }];

    NSLog(@"%@",sender);

}

//按键响应函数(收键盘)

- (IBAction)registe:(id)sender {

    [self.nameTextFieldresignFirstResponder];

    [self.passTextFieldresignFirstResponder];

    [UIView animateWithDuration:0.3animations:^{

        self.myButton.frame=oldFrame;

    }];

}

0 0
原创粉丝点击