键盘处理
2016-05-11 20:45
316 查看
键盘处理
退出键盘
[self.messageField resignFirstResponder] [self.messageField endEditing:YES] [self.view endEditing:YES]; // 推荐
叫出键盘
[self.messageField becomeFirstResponder]
更换键盘
UIView *keyboard = [[UIView alloc] init]; keyboard.frame = CGRectMake(0, 0, 0, 100); // 不用设置x,y,width keyboard.backgroundColor = [UIColor redColor]; self.emailField.inputView = keyboard;
设置键盘顶部的工具条;
// 加载工具条控件 UIToolbar *toolbar = [[[NSBundle mainBundle] loadNibNamed:@"AHKeyboardTool" owner:nil options:nil] firstObject]; // 设置工具条 self.nameField.inputAccessoryView = toolbar;
监听键盘通知
- (void)viewDidLoad { [super viewDidLoad]; // 设置文本框左边的内容 UIView *leftView = [[UIView alloc] init]; leftView.frame = CGRectMake(0, 0, 10, 0); self.messageField.leftView = leftView; self.messageField.leftViewMode = UITextFieldViewModeAlways; // 监听键盘通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - 键盘处理 - (void)keyboardWillChangeFrame:(NSNotification *)note { // 取出键盘最终的frame CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改transform [UIView animateWithDuration:duration animations:^{ CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y; self.view.transform = CGAffineTransformMakeTranslation(0, - ty); }]; }
UITextFieldDelegate
#pragma mark - <UITextFieldDelegate> /** * 键盘弹出就会调用这个方法 */ - (void)textFieldDidBeginEditing:(UITextField *)textField{ } /** * 当点击键盘右下角的return key时,就会调用这个方法 */ - (BOOL)textFieldShouldReturn:(UITextField *)textField{ }
相关文章推荐
- CQU 牛奶硬盘
- 一位算法工程师对自己工作的反思,写得挺实在的
- 列变位法解密
- openGL 解析ply文件并且显示三维模型
- 代理设计模式 && block设计模式 && KVC\KVO
- JDBC系列:(2)使用Statement执行sql语句
- Deep learning 资料汇总--持续更新
- 记录两个python的小问题
- tableView性能优化cell循环利用 && 自定义cell
- Cordova JqueryMoible 开发
- IOS程序开发-UI基础
- 堆的实现
- 循环结构
- 记录两个python的小问题
- EM算法介绍1
- 记录两个python的小问题
- cnblogs用户体验
- Git 常用命令速查表
- 杀掉进程
- delphi概念性学习(二)