禁用键盘上的键
我是Objective-C的新手,我正在寻求限制用户从普通键盘的字母部分切换到数字/标点符号。 这就是说,我想禁用键盘上的开关button。 也许我正在input错误的search参数,但是我在Google上发现的很less,在我的参考书中更less。 我将如何去实际禁用iOS键盘上的button? 使用字母/数字/标点字符时,只需忽略input的input字符即可轻松完成此操作,但是在键盘部分之间切换的button会执行操作而不是返回字符。
感谢您的帮助!
您实际上可以添加和删除默认UIKeyboard
button
在互联网上有一些食谱是这样的: http : //www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html ,像这样: http : //www.iphonedevsdk.com/论坛/ iphone-SDK开发/ 6275-插件工具栏机上keyboard.html
这些post告诉你如何添加一个button,但同样的原则可以用来删除。
下面我将向您展示一个解决scheme的汇编:
//The UIWindow that contains the keyboard view - //It some situations it will be better to actually //iterate through each window to figure out where the keyboard is, // but In my applications case //I know that the second window has the keyboard so I just reference it directly // UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] // objectAtIndex:1]; //Because we cant get access to the UIKeyboard throught the SDK we will // just use UIView. //UIKeyboard is a subclass of UIView anyways UIView* keyboard; //Iterate though each view inside of the selected Window for(int i = 0; i < [tempWindow.subviews count]; i++) { //Get a reference of the current view keyboard = [tempWindow.subviews objectAtIndex:i]; //Check to see if the className of the view we have //referenced is "UIKeyboard" if so then we found //the keyboard view that we were looking for if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) { // Keyboard is now a UIView reference to the // UIKeyboard we want. From here we can add a subview // to th keyboard like a new button //Do what ever you want to do to your keyboard here... } }
我实施了一个整洁的解决scheme。 诀窍是将禁用的关键图像放在键盘上。
去做这个
- 运行模拟器(100%的比例)和屏幕抓取你想要的资产(在我的情况下,这是一个禁用完成button在右下angular)
- 将此图像放在键盘上
请注意,键盘放置在一个单独的UIWindow(因为我相信iOS5),因此,您将需要执行以下操作
+ (void) addSubviewToTop:(UIView *)view { int count = [[[UIApplication sharedApplication] windows] count]; if(count <= 0) { warn(@"addSubviewToTop failed to access application windows"); } UIWindow *top_window = [[[UIApplication sharedApplication] windows] objectAtIndex:count-1]; [top_window addSubview:view]; [top_window bringSubviewToFront:view]; }
我不确定SDK是否发生了变化,使得@ ppaulojr的答案不再有效,或者我的系统中有些东西是奇怪的,但是通过下面的调整,我能够得到它的工作!
在@ ppaulojr的回答链接的post是伟大的( http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html和http://www.iphonedevsdk.com/forum/ iphone-sdk-development / 6275-add-toolbar-top-keyboard.html ),他们帮助我得到这个工作。
显然,实际的键盘视图现在被embedded在一些更macros观的UIKeyboard视图结构中的子视图,因此涉及到一些recursion。 我得到这个工作:
-(void) findKeyboard { NSArray* windows = [[UIApplication sharedApplication] windows]; for (int i = 0; i < [windows count]; i++) { UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:i]; for(UIView *subView in [tempWindow subviews]) { [self checkViews:subView]; } } } -(void)checkViews:(UIView *)inView { for(UIView *keyboard in inView.subviews) { NSLog( @"ViewName: %@", [keyboard description] ); // Which view are we looking at //Check to see if the className of the view we have //referenced is "UIKeyboard" if so then we found //the keyboard view that we were looking for if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) { // Keyboard is now a UIView reference to the // UIKeyboard we want. From here we can add a subview // to th keyboard like a new button //Do what ever you want to do to your keyboard here... break; } // Recurse if not found [self checkViews:subView]; } }
我也发现调用这个函数的最好的地方是从-(void)textViewDidBeginEditing:(UITextView *)textView
像这样:
- (void)textViewDidBeginEditing:(UITextView *)textView { NSLog(@"textViewDidBeginEditing"); [self findKeyboard]; }
一旦键盘被添加到窗口中,但是在它实际显示出来之前,键盘修改就会完成,所以从底部开始的整个时间将被修改。