隐藏键盘,在键盘上部添加一个取消键盘的按钮
2012-08-03 15:19
246 查看
首先在AppDelegate代理中添加一个通知方法
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(keyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
方法实现部分:
-(void)keyboardShow:(NSNotification *)notificaton
{
UIWindow* tempWindow = [[[UIApplication
sharedApplication]
windows]
objectAtIndex:1];//或者
[[[UIApplication sharedApplication] windows] lastObject];
for(int i=0; i<[tempWindow.subviews
count]; i++)
{
UIView * keyboard = [tempWindow.subviews
objectAtIndex:i];
NSString * string=[NSString
stringWithFormat:@"%@",[keyboard
class]];
if ([string
isEqual:@"UIPeripheralHostView"])//UIPeripheralHostView
{//UIKeyboardCornerView
UIButton * btn=[UIButton
buttonWithType:1];
[btn
setFrame:CGRectMake(220, -30,100 ,
30)];
[keyboard
addSubview:btn];
[btn
addTarget:self
action:@selector(resing:)
forControlEvents:UIControlEventTouchUpInside];
}
}
}
按钮调用的方法实现:
-(void)resing:(id)sender
{
UIWindow* window = [UIApplication
sharedApplication].keyWindow;
UIView *firstResponder = [window
performSelector:@selector(firstResponder)];
[firstResponder
resignFirstResponder];
}
这样不管在任何地方,只要键盘出来上面就会有一个按钮,点击该按钮就会取消键盘。
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(keyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
方法实现部分:
-(void)keyboardShow:(NSNotification *)notificaton
{
UIWindow* tempWindow = [[[UIApplication
sharedApplication]
windows]
objectAtIndex:1];//或者
[[[UIApplication sharedApplication] windows] lastObject];
for(int i=0; i<[tempWindow.subviews
count]; i++)
{
UIView * keyboard = [tempWindow.subviews
objectAtIndex:i];
NSString * string=[NSString
stringWithFormat:@"%@",[keyboard
class]];
if ([string
isEqual:@"UIPeripheralHostView"])//UIPeripheralHostView
{//UIKeyboardCornerView
UIButton * btn=[UIButton
buttonWithType:1];
[btn
setFrame:CGRectMake(220, -30,100 ,
30)];
[keyboard
addSubview:btn];
[btn
addTarget:self
action:@selector(resing:)
forControlEvents:UIControlEventTouchUpInside];
}
}
}
按钮调用的方法实现:
-(void)resing:(id)sender
{
UIWindow* window = [UIApplication
sharedApplication].keyWindow;
UIView *firstResponder = [window
performSelector:@selector(firstResponder)];
[firstResponder
resignFirstResponder];
}
这样不管在任何地方,只要键盘出来上面就会有一个按钮,点击该按钮就会取消键盘。
相关文章推荐
- 一个界面,有多个UITextField的时候,点击提交按钮发送数据时,取消键盘
- 切换控制器的时候隐藏搜索栏按钮,隐藏键盘,取消第一响应
- 给键盘添加一个tooBar,完成按钮,以及对写一个分类的理解
- iOS 数字输入键盘添加一个完成按钮
- 一个界面,有多个UITextField的时候,点击提交按钮发送数据时,取消键盘
- 重载确认对话框,点击确定触发一个隐藏按钮事件,点击取消触发一个隐藏按钮事件
- iOS 为键盘添加隐藏按钮
- 在键盘上方添加取消按钮
- 在GridView 中点击某一个按钮在此按钮的下行动态添加一行,再次点击第二次添加的行隐藏
- iOS隐藏键盘————给iOS键盘添加收起(隐藏)按钮
- ios隐藏数字键盘 --在数字键盘左下角添加“完成”按钮的实现原理
- 在GridView 中点击某一个按钮在此按钮的下行动态添加一行,再次点击第二次添加的行隐藏
- angular+做一个日程表,可以添加内容然后可以隐藏显示
- EXT扩展实例:在FieldSet上添加一个关闭按钮
- 在GridView里添加一个功能按钮
- ListView下面添加一个按钮(更多)加载更多数据
- 窗体上有两个文本框:一个文本框中最多输入字符6个;一个文本框中输入任何内容都显示*号。再添加一个按钮、2个单选按钮。实现单击按钮后,根据单选按钮,将对应文本框中内容显示在标签
- Android中点击Button按钮或响应业务处理后如何隐藏输入法键盘
- C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到