您的位置:首页 > 其它

键盘弹出时textfield也随着上移,键盘隐藏时,textfield回到原来的位置

2015-01-19 10:31 459 查看
blog.csdn.net/iorchid/article/details/6561862


键盘弹出时textfield也随着上移,键盘隐藏时,textfield回到原来的位置


_tspasswordTF = [[UITextField alloc] initWithFrame: CGRectMake(100, 150, 60, 30)];
_tspasswordTF.backgroundColor = [UIColor redColor];
_tspasswordTF.tag = 2;
_tspasswordTF.delegate = self;
[self.view addSubview: _tspasswordTF];

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self animateTextField: textField up: NO];
[textField resignFirstResponder];
return  YES;
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed

int movement = (up ? -movementDistance : movementDistance);

[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];

self.view.frame = CGRectOffset(self.view.frame, 0, movement);

[UIView commitAnimations];

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: