我的问题是: 当我写一个用户登录UI时,因为键盘隐藏了密码文本字段,所以我写了一些代码来向上滚动登录界面,但是当键盘被隐藏并且UI滚动到旧位置时,我发现我触摸了文本字段我无法得到任何回应。
注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillshow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillhide:)
name:UIKeyboardWillHideNotification
object:nil];
_username.delegate = self;
_password.delegate = self;
将显示键盘代码:
- (void) keyboardWillshow:(NSNotification *)noti
{
NSDictionary *userInfo = [noti userInfo];
NSValue *keyValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyValue CGRectValue];
int height = keyboardRect.size.height;
userInfo = nil;
keyValue = nil;
[UIView animateWithDuration:0.5f animations:^{
self.containView.frame = CGRectMake(0, -height+20, WIDTH/2, HEIGHT/2-20);
}];
}
将隐藏键盘代码:
- (void)keyboardWillhide:(NSNotification *)noti
{
[UIView animateWithDuration:0.5f animations:^{
self.containView.frame = CGRectMake(0, 20, WIDTH/2, HEIGHT/2-20);
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_password resignFirstResponder];
[_username resignFirstResponder];
}
有人可以帮助我吗?我使用故事板编写代码。