![](https://www.cnblogs.com/Emoticons/baimantou/202015214.gif)
//定义一个消息中心
[[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键盘颜色的代码。
![](http://www.cocoachina.com/bbs/attachment/Fid_6/6_1439_fcab8560f0e5b9c.png)
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];
- }
- }
- }
- }