Swift51.com
麦子学院 头像
麦子学院  2017-01-16 11:09

ios中如何自定义数字键盘

回复:0  查看:3184  

本文和大家分享的主要是ios开发中自定义数字键盘的相关知识,希望对大家有所帮助,一起来看看吧。

  第一种方式: 在获得键盘弹出通知时,在键盘的那个 UIView 上添加一个自定义的 UIButto

  #define KEY_WIDTH 106

  #define KEY_HEIGHT 53

  #pragma mark – 处理 TextField 响应事件

  – ( void )editingDidBegin:( UITextField *)textF {

  [ self . textF becomeFirstResponder ];

  }

  //3. 实现通知处理

  – ( void )handleKeyboardWillHide:( NSNotification *)notification

  {

  if ( doneInKeyboardButton . superview )

  {

  [ doneInKeyboardButton removeFromSuperview ];

  }

  }

  – ( void )handleKeyboardDidShow:( NSNotification *)notification

  {

  NSDictionary *info = [notification userInfo ];

  CGSize kbSize = [[info objectForKey : UIKeyboardFrameEndUserInfoKey ] CGRectValue ]. size ;

  CGFloat normalKeyboardHeight = kbSize. height ;

  int cnt = [[ UIApplication sharedApplication ] windows ]. count ;

  UIWindow * tempWindow = [[[ UIApplication sharedApplication ] windows ] objectAtIndex :cnt- 1 ];

  // create custom button

  if ( doneInKeyboardButton == nil )

  {

  doneInKeyboardButton = [ UIButton buttonWithType : UIButtonTypeCustom ];

  doneInKeyboardButton . frame = CGRectMake ( 18 , tempWindow. frame . size . height – 53 , 106 , 53 );

  doneInKeyboardButton . adjustsImageWhenHighlighted = NO ;

  [ doneInKeyboardButton setImage :[ UIImage imageNamed : @”done.png” ] forState :UIControlStateNormal ];

  [ doneInKeyboardButton setImage :[ UIImage imageNamed : @”done.png” ] forState :UIControlStateHighlighted ];

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

  }

  // locate keyboard view

  if ( doneInKeyboardButton . superview == nil )

  {

  // 注意这里直接加到 window 

  [tempWindow addSubview : doneInKeyboardButton ];

  }

  }

  #pragma mark – 处理视图响应事件

  – ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event {

  [ self . textF resignFirstResponder ];

  }

  -( void )viewWillAppear:( BOOL )animated

  {

  [ super viewWillAppear :animated];

  //1. 先注册通知 [[ NSNotificationCenter defaultCenter ] addObserver : self selector :@selector (handleKeyboardDidShow:) name : UIKeyboardDidShowNotification object :nil ];

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

  }

  //2. 在 dealloc 中反注册通知

  -( void )dealloc

  {

  [[ NSNotificationCenter defaultCenter ] removeObserver : self ];

  }

  第二种方法:自己写一个键盘。:laughing:

 

来源:海之飞燕