iOS 自定义键盘(配Demo下载)
来源:互联网 发布:学美工要多长时间 编辑:程序博客网 时间:2024/04/29 15:03
iOS自定义键盘Demo下载地址 : http://download.csdn.net/detail/lovechris00/9646238
1、首先自定义一个View作为自定义键。你可以继承自UIView,也可以继承自你自定义的类。
同时可以自定义一个toolBar。
@property (nonatomic,strong) UITextView *txView; //输入源@property (nonatomic,strong) UIView *customKeyboard; //自定义键盘@property (nonatomic,strong) UIView *keyboardBar; //键盘上工具栏
-(UITextView *)txView{ if (!_txView) { _txView = [[UITextView alloc]init]; _txView.frame = CGRectMake(15, 15, kSCREEN_WIDTH - 30, 200); } return _txView;}-(UIView *)customKeyboard{ if (!_customKeyboard) { _customKeyboard = [[UIView alloc]init]; _customKeyboard.backgroundColor = [UIColor cyanColor]; _customKeyboard.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 200); } return _customKeyboard;}-(UIView *)keyboardBar{ if (!_keyboardBar) { _keyboardBar = [[UIView alloc]init]; _keyboardBar.frame = CGRectMake(0, kSCREEN_HEIGHT - 44, kSCREEN_WIDTH, 44); _keyboardBar.backgroundColor = [UIColor redColor]; _keyboardBar.hidden = YES; UIButton *btn = [[UIButton alloc]init]; btn.frame = CGRectMake(kSCREEN_WIDTH - 100, 0, 100, 44); btn.backgroundColor = [UIColor lightGrayColor]; [btn setTitle:@"切换键盘" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(changeKeyboard) forControlEvents:UIControlEventTouchUpInside]; [_keyboardBar addSubview:btn]; } return _keyboardBar;}
2、添加键盘监听方法,并注意销毁通知;
- (void)registerNotification{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; }- (void)removeNotification{ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];}
#pragma mark - 键盘处理- (void)keyboardWillHide:(NSNotification *)note{ CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ self.keyboardBar.transform = CGAffineTransformIdentity; }]; }- (void)keyboardWillShow:(NSNotification *)note{ CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardH = keyboardF.size.height; self.keyboardBar.transform = CGAffineTransformMakeTranslation(0, - keyboardH); }];}- (void)keyboardDidShow:(NSNotification *)notification{ NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]]; CGFloat keyboardHeight = keyboardFrame.size.height; self.keyboardBar.hidden = NO; NSLog(@"##keboardHeight=%.2f -%@",keyboardHeight,self.txView.inputView);}
3、修改textView的inputView, 如果把inputView置空,则会显示为系统键盘;
每次修改键盘时,需要取消输入源的第一响应。
- (void)changeKeyboard{ if (self.txView.inputView) { self.txView.inputView = nil; //设置为系统键盘 }else{ self.txView.inputView = self.customKeyboard; } [self.txView resignFirstResponder]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.txView becomeFirstResponder]; });}
iOS自定义键盘Demo下载地址 : http://download.csdn.net/detail/lovechris00/9646238
温馨提示:
尽量用真机进行测试。模拟器测试时,请注意键盘的弹出状态。
iOS自定义键盘Demo下载地址 : http://download.csdn.net/detail/lovechris00/9646238
iOS自定义键盘Demo下载地址 : http://download.csdn.net/detail/lovechris00/9646238
iOS自定义键盘Demo下载地址 : http://download.csdn.net/detail/lovechris00/9646238
iOS自定义键盘Demo下载地址 : http://download.csdn.net/detail/lovechris00/9646238
0 0
- iOS 自定义键盘(配Demo下载)
- iOS 简单日历(配Demo下载)
- iOS录音方法实用详解(配Demo下载)
- iOS UICollectionViewCell拖拽移动(配Demo下载)
- iOS 时间轴相册 (配Demo下载)
- iOS tableView无限展开(配Demo下载)
- iOS 音乐播放Tool (配Demo下载)
- iOS 图片剪裁(附demo下载)
- Objective-C-如何自定义键盘(iOS)
- Swift-如何自定义键盘(iOS)
- iOS - 键盘自定义:
- iOS 自定义键盘
- IOS自定义键盘
- ios自定义键盘高度
- iOS 自定义键盘
- iOS自定义键盘
- iOS 添加自定义键盘
- iOS开发-自定义键盘
- Android 属性动画
- Android编程权威指南(第二版)学习笔记(一)——第1章 Android开发初体验
- android横竖屏切换,activity生命周期方法是否会回调
- Atitit 游戏的原理与概论attilax总结
- Eclipse添加混淆文件
- iOS 自定义键盘(配Demo下载)
- 冒泡排序之前奏---“潜伏”
- Readis 设置密码登录
- requireJs的粗浅记录一
- 机器人概率定位学习笔记第二篇_使用python建立机器人感知函数(sense fuction)
- Eclipse的安卓项目中出现红色叹号怎样解决
- linux中shell变量$#,$@,$$,$!,$-,$*,$n的意义
- 04.Spring Ioc 容器 - 刷新
- 数组矩阵