如何创建停留在屏幕底部的可移动UItextfield,当键盘出现时,它会移动到iphone应用程序中键盘的顶部?怎么在Xcode 4.2中做到这一点? 就像在whatsapp和Skype聊天一样。 我想用来将字符串输入到表中。
答案 0 :(得分:1)
您应该将viewController注册为键盘通知的侦听器。当键盘出现时,会使用用户词典触发通知。字典将包含有用的信息,例如相对于屏幕的键盘位置,用于将textFields帧设置为新位置。查看文件:
具体来说,您需要的通知是:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
在大多数情况下,您需要在viewDidLoad中添加这些内容。并且不要忘记在完成后取消注册通知(removeObserver :)。例如在viewDidUnload中。
答案 1 :(得分:0)
您可以使用Hubert提到的通知,或者您可以不断检查UITextField是否是第一个响应者。如果它是第一个响应者,这意味着选择了文本字段,键盘将影响该字段。
-(void)viewDidLoad {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(checkTextField) userInfo:nil repeats:YES];
}
-(void)checkTextField {
if (textField isFirstResponder) {
//the text field has been tapped and the keyboard will come up, so animate the text field moving up here
} else {
//the text field is not selected, so it should be in its original position
}
}