如何在IOS虚拟键盘上添加动态隐藏按钮

悟途网 2013年06月06日 22:25 阅读()
字号 (A- A+)

想在键盘上添加一个按钮,实时根据键盘不同高度变换按钮位置,再不做输入的时候点击按钮能够隐藏键盘,这种方式在很多软件上都有体现,然后在网上查阅了关于检测键盘高度一些相关知识,以下是一个Demo,代码有很多需要优化地方,仅供需要者参考;

 
先看效果:
IOS虚拟键盘上添加动态隐藏按钮   IOS虚拟键盘上添加动态隐藏按钮 
 
IOS虚拟键盘上添加动态隐藏按钮 IOS虚拟键盘上添加动态隐藏按钮 
 
 
首先是我们在ViewDidLoada()中注册了两个通知,[NSNotificationCenterdefaultCenter],检测键盘动态,一个是键盘将要弹出的时候,另一个是键盘将要退出时候键盘的信息
- (void)viewDidLoad
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    [super viewDidLoad];
	
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];  
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
检测键盘消息一个六种,根据字面意思差不多都能说明函数作用
UIKeyboardWillShowNotification     通知将要发布时候显示键盘 
UIKeyboardDidShowNotification    通知发布后立即显示键盘
UIKeyboardWillHideNotification       通知发布前撤销键盘
UIKeyboardDidHideNotification       通知发布后撤销键盘
UIKeyboardWillChangeFrameNotification      通知发布前迅速变化的框架的键盘。
UIKeyboardDidChangeFrameNotification      通知发布后立即改变在键盘的框架。
 
 
NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制台显示打印出当前程序所调用的函数,我在下面每个函数都加了这一句,当我进行不同操作的时候,打印出被调用函数名,在调试程序时候比较适用吧;
IOS虚拟键盘上添加动态隐藏按钮
 
注册消息通知后,实现通知所响应的方法
 
- (void)handleKeyboardDidShow:(NSNotification *)notification 
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    
    if (exitButton == nil) {
        exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f);
        exitButton.frame = exitBtFrame;
        [exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal];
        [self.view addSubview:exitButton];
        
    }
    exitButton.hidden=NO;
    
    [self adjustPanelsWithKeyBordHeight:distanceToMove];
    
    [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];
    

}
在这个函数方法中值得探讨的是关于键盘所包含信息,因为每一次键盘弹出的时候也是动画形式弹出,他的坐标位置大小包含在userInfo的字典中,现在我用
NSLog(@"-->info:%@",info);打印出info对象,这些信息都可以在不同存储类型,取值的时候注意取值方式,此处只是提一提,希望以后有时间在做探讨,
IOS虚拟键盘上添加动态隐藏按钮

在这一段代码上,后面注释了5行,因为打算当键盘推出的时候,按钮从视图上移除,或者释放按钮,但是都导致了应用程序崩溃,后来就没有释放和移除操作了

- (void)handleKeyboardWillHide:(NSNotification *)notification 
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    if (exitButton.hidden==NO) {
        exitButton.hidden = YES;
    }
    
//    if (exitButton.superview) 
//    {
//        [exitButton removeFromSuperview];
//        [exitButton release];
//    }

    
}
-(void)adjustPanelsWithKeyBordHeight:(float) height
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    if (exitButton) {

       CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f);
        exitButton.frame = exitBtFrame;

        [self.view addSubview:exitButton];

    }
    
                        
//    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
//    if (exitButton.superview == nil) 
//    {
//        [tempWindow addSubview:exitButton];
//        // 注意这里直接加到window上
//    }
    
}
-(void)CancelBackKeyboard:(id)sender
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    
    [textField resignFirstResponder];
    
}


- (void)viewDidUnload
{
    [self setTextField:nil];
    exitButton=nil;
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
}


- (void)dealloc {
    [textField release];
    [exitButton release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所注册的通知
    [super dealloc];
}

热门文章
随机推荐
苹果iPhone手机手电筒亮度调节教程

苹果iPhone手机手电筒亮度调

苹果在最新的iOS10系统中带来了不少改进,其中iPhone6...

iPhone6s有必要升级iOS11系统吗?iOS11好用吗?

iPhone6s有必要升级iOS11系统吗

iPhone6s升级iOS10.3.2好用吗?要不要升级iOS11呢?想必有很...

怎么在ios代码控制出现控件的阴影

怎么在ios代码控制出现控件

怎么在ios代码控制出现控件的阴影,只需要把对应的空...

iPhone手机怎么设置一个回电提醒的方法

iPhone手机怎么设置一个回电

来电并不总是在适当的时候出现,如果你的iPhone在开会...

iOS 9越狱后必备插件汇总

iOS 9越狱后必备插件汇总

iOS9必备插件有哪些?盘古大神iOS9完美越狱出来啦,越...

iPhone小圆点AssistiveTouch使用大全

iPhone小圆点AssistiveTouch使用

小圆点AssistiveTouch并不是随iPhone出生一起诞生的,直到...

如何更快更好用iPhone6加速优化方法

如何更快更好用iPhone6加速优

怎样在不影响主体功能的前提下尽量提高iPhone6的性能呢...

防止孩子删除iPhone手机App应用的方法

防止孩子删除iPhone手机App应

随着iPhone的普及,经常可以看到玩iPhone手机的孩子。在...