iOS8数字键盘左下角添加完成按钮

iOS8数字键盘左下角添加完成按钮的核心代码如下:

- (void)addDoneButtonToNumPadKeyboard
{
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (systemVersion < 8.0){
        doneButton.frame = CGRectMake(0, 163, 106, 53);
    }else{
        doneButton.frame = CGRectMake(0, SCREEN_SIZE.height-53, 106, 53);
    }
    doneButton.tag = NUM_PAD_DONE_BUTTON_TAG;
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

    NSArray *windowArr = [[UIApplication sharedApplication] windows];
    if (windowArr != nil && windowArr.count > 1){
        UIWindow *needWindow = [windowArr objectAtIndex:1];
        UIView *keyboard;
        for(int i = 0; i < [needWindow.subviews count]; i++) {
            keyboard = [needWindow.subviews objectAtIndex:i];
            if(([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) || ([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)){
                if ([[keyboard subviews] count] <= 1){
                    [keyboard addSubview:doneButton];
                }
            }
        }
    }
}

-(void)removeDoneButtonFromNumPadKeyboard
{
    UIView *doneButton = nil;

    NSArray *windowArr = [[UIApplication sharedApplication] windows];
    if (windowArr != nil && windowArr.count > 1){
        UIWindow *needWindow = [windowArr objectAtIndex:1];
        UIView *keyboard;
        for(int i = 0; i < [needWindow.subviews count]; i++) {
            keyboard = [needWindow.subviews objectAtIndex:i];
            if(([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) || ([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)){
                doneButton = [keyboard viewWithTag:NUM_PAD_DONE_BUTTON_TAG];
                if (doneButton != nil){
                    [doneButton removeFromSuperview];
                }
            }
        }
    }
}

注:

1.iOS8之后,键盘view的description变为以<UIInputSetContainerView开头,所以需要在判断的逻辑中加上这一项。

2.iOS8之后,键盘view的大小变成了整个屏幕的大小,所以需要对按钮添加的位置进行适配。

时间: 2024-08-26 14:28:46

iOS8数字键盘左下角添加完成按钮的相关文章

ios在数字键盘左下角添加“完成”按钮的实现原理

最近要在系统弹出的数字键盘上的左下角额外添加一个自定制的完成按钮,于是研究了一下系统自带键盘添加自定制按钮的实现方式.总结了一下大体上的通用做法,原理大概是这样:当页面上的文本框或其他输入源因为用户的点击而变成第一响应者的时候(becomeFirstResponder),系统键盘就会弹出.而每次键盘弹出或收起时,都会向系统发送相关的键盘事件即通知消息(notification).所以,我们只要在键盘弹出或收起时捕获相关的键盘事件,并且在键盘对应的window上的相应位置添加或移除我们自定制的按钮

iOS8数字键盘加左下角完成button

iOS8数字键盘加左下角完成button的核心代码如下面: - (void)addDoneButtonToNumPadKeyboard { UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; if (systemVersion < 8.0){ doneButton.frame = CGRectMake(0, 163, 106, 53); }else{ doneButton.frame = CGRectMake(

iOS数字键盘自定义按键

UIKeyboardTypeNumberPad 数字键盘自定义按键 最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: self.searchBar.keyboardType = UIKeyboardTypeNumberPad; 如果使用的不是搜索框而是textField输入框,可以设置textField的键盘属性来展示 self.textField.keyboardType = UIKeyboardTypeNum

iOS 为键盘添加隐藏按钮

// 为键盘添加隐藏按钮 UIToolbar * backView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; [backView setBarStyle:UIBarStyleDefault]; UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSp

基于jQuery的数字键盘插件

有时,我们需要在网页上使用软键盘.今天,就给大家带来一个基于jQuery的数字键盘插件,除了jQuery,不需要依赖任何文件资源.纯数字键盘,有退格,有清除,不支持输入小数(需要的可以自己改一下,主要是多个小数点就有13个键,不好排列了,呵呵).支持鼠标拖动和触摸拖动,可关闭. 在线演示 1.页面代码 <ul> <li><input type="text" placeholder="手机号码后四位" id="numkeyboa

&lt;Android&gt;监听软键盘打开收起事件(软键盘自带收起按钮)

最近在公司开发cocos2dx上的android输入框控件,遇到软键盘的事件监听,通常软键盘的收起方式大致3种: 1.点击软键盘右下角的Return按钮(系统收起) 2.输入框焦点时按返回按钮(系统收起) 3.点击软键盘和输入框的外部(自发收起) 4.点击软键盘自带的收起按钮(软键盘收起) 前三种事件可以监听,方式都比较简单 1.点击软键盘右下角的Return按钮 给输入框设置监听 editText.setOnEditorActionListener(new OnEditorActionList

Android 自定义数字键盘

业务需求自定义数字键盘,写了一个demo 主要代码: import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; import android.vi

【笔记】移动端H5数字键盘input type=number的处理(IOS和Android)

在Vue中的项目,基于VUX-UI开发,一个常见的需求: 1.金额输入框 2.弹出数字键盘 3.仅支持输入两位小数,限制最大11位数,不允许0开头 第一,首先想到额就是在VUX-UI中制定type=number.--不可行 VUX中的文档和代码说明,type=number不支持maxLength,会报错,而且没有正则替换的处理或者钩子函数,只有输入后提示校验信息. 第二,基于VUX中XInput封装,有如下问题 1)两层v-model,正则替换的值不会触发input框渲染 解决:currentV

js输入密文弹出数字键盘

我们经常被产品要求,在移动端的web页面上的输入框输入密码时要弹出数字键盘,而不是全键盘,这个该怎么实现呢? 1.首先要弹出数字键盘,我们只能把input框的type从password改为tel 2.但经过第一步,输入的内容会变成明文,这时候也要星号显示,改怎么实现 经过一番研究,找到如下的实现方法: 1 function setPass(e) { 2 var target = e.currentTarget, 3 idx = target.selectionStart, 4 val = $(t