一.UITextFiled收回键盘
首先设置textfiled的delegate为self
然后实现textFieldShouldReturn这个代理
textFieldShouldReturn会在会用点击return的时候调用,return的值表示系统是否要采取默认的行为。
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
二.UITextView收回键盘
首先设置textview的delegate为self,然后实现textView shouldChangeTextInRange这个代理
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
这个代理会在用户输入或者删除文字时调用,textfiled也有这个代理,也可以通过这种方式实现textfiled收回键盘
三.当键盘弹起时自动调整输入框的位置
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
-(void)KeyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
//获取高度
NSValue *value = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];
CGSize keyboardSize = [value CGRectValue].size;
float keyboardHeight = keyboardSize.height;
// 获取键盘弹出的时间
NSValue *animationDurationValue = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//自定义的frame大小的改变的语句
…………….
}
网友评论