您的位置:首页 > 移动开发 > IOS开发

ios8数字键盘添加完成

2014-12-25 22:44 357 查看
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(addDoneButtonToNumPadKeyboard)
name:UIKeyboardDidShowNotification
object:nil];

//注册键盘隐藏通知
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(removeDoneButtonFromNumPadKeyboard)
name:UIKeyboardDidHideNotification
object:nil];

- (void)addDoneButtonToNumPadKeyboard
{

UIButton *doneButton = [UIButton
buttonWithType:UIButtonTypeCustom];

if ([[[UIDevice
currentDevice] systemVersion]
floatValue] < 8.0){
doneButton.frame =
CGRectMake(0,
163, 106,
53);
}else{
doneButton.frame =
CGRectMake(0,
self.view.frame.size.height-53,
106, 53);
}
doneButton.tag =
20;

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];

NSLog(@"%@", [keyboard
description]);

if(([[keyboard description]
hasPrefix:@"<UIPeripheralHostView"] ==
YES) || ([[keyboard
description] hasPrefix:@"<UIKeyboard"] ==
YES) || ([[keyboard
description] hasPrefix:@"<UIInputSetContainerView"] ==
YES)){

UIView *doneButtonView = [keyboard
viewWithTag:20];

if (doneButtonView ==
nil){
[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:20];

if (doneButton !=
nil){
[doneButton
removeFromSuperview];
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: