`
fosa0989
  • 浏览: 108532 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ios: 一个用户自定义键盘的例子

阅读更多

 If you have ever written an iPhone app that requires numeric input, then you surely know about the UIKeyboardTypeNumberPad. And if you have ever used that flavor of the iPhone's keyboard, then you surely know that it lacks one very important feature: The UIKeyboardTypeNumberPad does not have a "return" key.

In fact every other keyboard type (except for the pretty similar UIKeyboardTypePhonePad) does offer the possibility to be dismissed by setting the returnKeyType property of the corresponding UITextInputTraits implementor. So how does one achieve the same effect with the number pad? We have found a workround!

When looking at the number pad, you'll notice that there is an unused space on its bottom left. That's where we are going to plug in our custom "return" key.

To make it short: take a screenshot, cut out the whole backspace key, flip it horizotally, clear its backspace symbol in Photoshop and overlay it with the text that we want on our “return” key. We’ve chosen to label it “DONE”. Now we have the image for our custom button’s UIControlStateNormal. Repeat the whole procedure (with a touched backspace key when taking the screenshot) to get a second image for our button’s UIControlStateHighlighted. Here’s the result:

 

 

Now back to coding. First we need to know when the number pad is going to be slided up on the screen so we can plug in our custom button before that happens. Luckily there’s a notification for exactly that purpose, and registering for it is as easy as:

 
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];
 

Don't forget to remove the observer from the notification center in the appropriate place once you're done with the whole thing:

 
[[NSNotificationCenter defaultCenter] removeObserver:self];

Now we’re getting to the heart of it. All we have to do in the keyboardWillShow method is to locate the keyboard view and add our button to it. The keyboard view is part of a second UIWindow of our application as others have already figured out (see this thread). So we take a reference to that window (it will be the second window in most cases, so objectAtIndex:1 in the code below is fine), traverse its view hierarchy until we find the keyboard and add the button to its lower left:

 
- (void)keyboardWillShow:(NSNotification *)note {  
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
 
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }
}

Voilà, that’s it! The empty space for our button starts at coordinate (0, 163) and has the dimensions (106, 53). The doneButton method has to be written now of course, but that’s not hard any more. Just make sure that you call resignFirstResponder on the text field that is being edited to have the keyboard slide down.

 

分享到:
评论

相关推荐

    TKKeyBoard:自定义键盘

    让我们从一个简单的例子开始 UITextField *textField = [[UITextField alloc ] init ]; textField.keyboardType = TKKeyboardTypeIntegerPad; [ self .view addSubview: textField]; [textField release ]; 这...

    【吐血推荐】IOS学习27个强悍实例

    【吐血推荐】IOS学习27个强悍实例 ViewTransitions(图片各方向切换) ViewSendData(弹出键盘) Unicode(编码) UITableViewDelteMutilRows(删除行) UIActivityIndicatorView(等待控件) turnPage(十种图层切换效果) ...

    zhPopupController:帮助您轻松弹出自定义视图。 并支持弹出动画,布局位置,蒙版效果和手势交互等

    它支持自定义弹出动画,布局位置,蒙版效果,键盘监视,手势交互等。它的API简单易用。版本2.0 zhPopupController 2.0版已经过优化和重构。 1.0版中的某些方法和属性不再兼容。 请谨慎升级。 迅捷版更加轻巧简洁。 ...

    TiDAKeyboardControl:TiDAKeyboardControl 为 Titanium SDK 中的键盘提供了一些高级行为,感谢 @danielamitay (iOS)

    您现在可以响应键盘事件(显示、隐藏和实时交互式更改)并自定义在交互式平移期间将用作偏移量的空间量。 来自的超棒Titanium SDK 包装器! (仅限 iOS) 安装 您可以使用安装此模块 gittio install it.smc....

    JGProgressHUD:适用于iOS和tvOS的优雅,简单的Progress HUD,与Swift和ObjC兼容

    易于自定义(自定义动画,指示器视图等)。 使用UIVisualEffectView和UIMotionEffect获得本机外观。 使用自动版式提供完全动态的版式。 出现/消失键盘时检测并重新定位。 暗模式支持-自动更改外观。 充分记录...

    searchable_dropdown

    用于让用户搜索在单个或多个选择列表中显示在对话框或菜单中的下拉列表的可自定义键盘上键入的关键字字符串的小部件。 平台类 此小部件已在iOS,Android和Chrome上成功测试。 例子 以下示例摘自的。 画廊 请参阅...

    Android-app.rar

    100多个Android的实用开源小应用,包含Android -- 引导页面的实现 Android APP引导页大全 Android Push it单机版事件管理APP Android RecycleView+任意头尾布局+拖拽换位+拖拽删除demo Android sql练习合集 Android ...

    JAVA上百实例源码以及开源项目

     当用户发送第一次请求的时候,验证用户登录,创建一个该qq号和服务器端保持通讯连接得线程,启动该通讯线程,通讯完毕,关闭Scoket。  QQ客户端登录界面,中部有三个JPanel,有一个叫选项卡窗口管理。还可以更新...

    JAVA上百实例源码以及开源项目源代码

    多人聊天+用户在线 21个目标文件 摘要:JAVA源码,媒体网络,山寨QQ,Java聊天程序 Java编写的山寨QQ,多人聊天+用户在线,程序分服务端和客户端,典型C/S结构, 当用户发送第一次请求的时候,验证用户登录,创建一个该...

Global site tag (gtag.js) - Google Analytics