iPhone自定义键盘
来源:互联网 发布:js如何实现倒计时功能 编辑:程序博客网 时间:2024/06/05 15:57
在数字键盘上添加button:
//定义一个消息中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //addObserver:注册一个观察员 name:消息名称
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
[doneButton setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(addRadixPoint) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//返回应用程序window
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) //遍历window上的所有subview
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
Class at a Glance
The NSNotificationCenter class provides a way to send notifications to objects in the same task. It takes NSNotification objects and broadcasts them to any objects in the same task that have registered to receive the notification with the task’s default notification center.
即全局变量一般,在同一个程序中,只要定义了一个通知,如果没有 remove遇到相同对象,同样的消息(入上面代码中的UIKeyboardWillShowNotification)同样的方法都回被调用(入上面代码中的:keyboardWillShow:)。
所以使用完毕后最好 removeObserver
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
- (void)keyboardWillShow:(NSNotification*)aNotification {
if (!maskViewShow){
//if(maskView != nil){
[maskView removeFromSuperview];
//[maskView release];
//maskView = nil;
//}
return;;
}
NSDictionary* info = [aNotification userInfo];
/* 获取键盘区域的CGRect值 */
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect maskRect ;//=[CGRect alloc];
[aValue getValue:&maskRect]; //宽高
//[bValue getValue:&maskRect]; //中心点坐标
//maskRect.origin
//maskView =[[UIView alloc] initWithFrame: maskRect];
maskView.frame = maskRect;
maskView.alpha =0.7;
maskView.backgroundColor = [UIColor blackColor] ;
//[.view addSubview:maskView];
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
/*便利window 上的所以view 找到 UIKeyboard 添加maskView*/
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
//NSLog(@"%@",[keyboard description]);
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
[keyboard addSubview:maskView];
break;
}
}
}
这样自定义键盘在点Done时,如果只执行 [curTextField resignFirstResponder]; 不会触发控制类的textFieldShouldReturn 委托方法,解决办法:
在 [curTextField resignFirstResponder]之前,调用textFieldShouldReturn
if ([curViewController respondsToSelector:@selector(textFieldShouldReturn:)]){
[curViewController performSelector:@selector(textFieldShouldReturn:) withObject:curTextField];
}
改变 iPhone键盘颜色的代码
原帖地址 http://www.cocoachina.com/bbs/read.php?tid-12244.html
苹果iPhone和iPod touch的键盘颜色其实是可以通过代码更改的,这样能更匹配您App的界面风格,下面是改变iPhone键盘颜色的代码。
1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2. 。keyboardAppearance = UIKeyboardAppearanceAlert
- - (void)textViewDidBeginEditing:(UITextView *)textView{
- NSArray *ws = [[UIApplication sharedApplication] windows];
- for(UIView *w in ws){
- NSArray *vs = [w subviews];
- for(UIView *v in vs){
- if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"]){
- v.backgroundColor = [UIColor redColor];
- }
- }
- }
- }
- iPhone自定义键盘
- iPhone 自定义键盘按键
- iPhone 自定义键盘按键
- iPhone键盘自定义及通知
- iPhone开发笔记 退回输入键盘:自定义颜色:隐藏状态栏:
- iPhone开发笔记 退回输入键盘:自定义颜色:隐藏状态栏:
- 自定义键盘
- 自定义键盘
- 自定义键盘
- 键盘自定义
- 自定义键盘
- 自定义键盘
- 自定义键盘
- 自定义键盘
- 自定义键盘
- 自定义键盘
- 自定义键盘
- 自定义键盘
- Javascript设置光标位置到结尾
- 歌诗达邮轮之旅(官方网站)
- QueryCommand的高效使用体现在动态参数赋值上:
- Sql 数据类型详解
- WSAEventSelect模型
- iPhone自定义键盘
- 寄辛幼安和见怀韵
- 由JSESSIONID谈cookie与SESSION的区别和联系
- papervision3d学习笔记:图片墙(2)
- 正则表达式中/i,/g,/ig,/gi,/m的区别和含义
- x86 寄存器
- 指向结构体的指针和指向结构体指针的指针
- 数据库事务简介
- 终结VS2005分发版本