• 自定义键盘


    在数字键盘上添加button:
    //定义一个消息中心
    [[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键盘颜色的代码

    作者  bright

    原帖地址  http://www.cocoachina.com/bbs/read.php?tid-12244.html

    苹果iPhone和iPod touch的键盘颜色其实是可以通过代码更改的,这样能更匹配您App的界面风格,下面是改变iPhone键盘颜色的代码。


    1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
    2. 。keyboardAppearance = UIKeyboardAppearanceAlert
    1. - (void)textViewDidBeginEditing:(UITextView *)textView{
    2.     NSArray *ws = [[UIApplication sharedApplication] windows];
    3.     for(UIView *w in ws){
    4.         NSArray *vs = [w subviews];
    5.         for(UIView *v in vs){
    6.             if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"]){
    7.                 v.backgroundColor = [UIColor redColor];
    8.             }
    9.         }
    10.     }
    11. }
  • 相关阅读:
    JavaScript——BOM和DOM
    css-2
    Css-1
    storage size of 'xxx' isn't known问题出现的可能原因之一
    解决VS2010中winsock.h与winsock2.h冲突(重复定义)——转载
    SQLite : 解决“找不到请求的 .Net Framework 数据提供程序。可能没有安装”的问题
    使用 VirtualBox 虚拟机在电脑上运行 Android 4.0 系统,让电脑瞬间变安卓平板
    C#连接ACCESS的一个问题
    对硅谷和硅谷科技公司的十四问,全程干货
    nginx源码学习资源
  • 原文地址:https://www.cnblogs.com/mac_arthur/p/1652407.html
Copyright © 2020-2023  润新知