【在数字键盘上添加button】

来源:互联网 发布:网红淘宝店质量 编辑:程序博客网 时间:2024/04/27 15:17

一、遍历添加

//定义一个消息中心 

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


二、直接添加到window上

因为的UIKeyboardTypeNumberPad类型的键盘没有完成按钮,为了自己加这个按钮,网上有N种方法,大体的思路就是:在获得键盘弹出通知时,在键盘的那个UIView上添加一个自定义的UIButton,但是这些方法都存在问题:

1.使用UIKeyboardWillShowNotification时,我在iphone4.35.0模拟器上发现keyboard视图还没有创建出来

2.ios5上,要根据不同的uiview description来找到keyboard,版本不同descruption不同

3.ios5上,添加UIButton的位置不同(x,y坐标都为负数才能显示按钮),并且按钮加上后不能被点击


折腾一上午,终于找到了方法,很简单,不用去找那个keyboard了,直接在keyboard的那个window上加button,直接就搞定了ios的所有版本。


方法如下:


1. 先注册通知

[[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(handleKeyboardDidShow:)name:UIKeyboardDidShowNotification object:nil];  

    

    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(handleKeyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];


2. dealloc中反注册通知

[[NSNotificationCenter defaultCenterremoveObserver:self];



3.实现通知处理

- (void)handleKeyboardWillHide:(NSNotification *)notification 

{

    if (doneInKeyboardButton.superview

    {

        [doneInKeyboardButton removeFromSuperview];

    }

    

}


- (void)handleKeyboardDidShow:(NSNotification *)notification  

{  

    NSDictionary *info = [notification userInfo];  

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKeyCGRectValue].size;  

    normalKeyboardHeight = kbSize.height;

    

    

    // create custom button

    if (doneInKeyboardButton == nil

    {

        doneInKeyboardButton = [[UIButton buttonWithType:UIButtonTypeCustomretain];

        

        doneInKeyboardButton.frame = CGRectMake(0480 - 5310653);

        

        doneInKeyboardButton.adjustsImageWhenHighlighted = NO;

        [doneInKeyboardButton setImage:[UIImage imageNamed:@"numPadDoneUp.png"]forState:UIControlStateNormal];

        [doneInKeyboardButton setImage:[UIImage imageNamed:@"numPadDoneDown.png"]forState:UIControlStateHighlighted];

        [doneInKeyboardButton addTarget:self action:@selector(finishAction)forControlEvents:UIControlEventTouchUpInside];

    }

    

    // locate keyboard view

    UIWindow* tempWindow = [[[UIApplication sharedApplicationwindowsobjectAtIndex:1];

    

    

    

    if (doneInKeyboardButton.superview == nil

    {

        [tempWindow addSubview:doneInKeyboardButton];// 注意这里直接加到window上

    }

    

}  



这样就搞定了,是不是很简单?总体上还是那个思路,但网上的方法细节上有些误导。


0 0
原创粉丝点击