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
原创粉丝点击