监听键盘改变

https://www.zhihu.com/question/20350598  键盘类型


-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
    [self registerForKeyboardNotifications];
}

/**
 * 注册通知
 */
- (void)registerForKeyboardNotifications
{
   //使用NSNotificationCenter 键盘出現时
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
     
 //使用NSNotificationCenter 键盘隐藏时
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
//键盘frame改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFDidChangeStatus:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
/**
 * 键盘显示的时候
 *  @param aNotification a
 */
- (void)keyboardWasShown:(NSNotification*)aNotification
{   
 NSDictionary* info = [aNotification userInfo]; 
 //kbSize即為鍵盤尺寸 (有width, height)
 CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
 NSLog(@"hight_hitht:%f",kbSize.height);
     [UIView animateWithDuration:.3 animations:^{
        
    }];
}

/**
 * 当键盘隐藏的时候
 *  @param aNotification a
 */
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
   NSLog(@"键盘隐藏le");
    [UIView animateWithDuration:.3 animations:^{
               
    }];
}

/////到这里

//textView  return收回键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
   if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
       return NO;
    }
   return YES;
}



//数字键盘完成按钮
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 35)];
    toolbar.tintColor = [UIColor blueColor];
    toolbar.backgroundColor = [UIColor grayColor];
//    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField)];
//    UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"上一步" style:UIBarButtonItemStylePlain target:self action:@selector(prevTextField)];

   
 UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
[bar setTintColor:[UIColor redColor]];//    toolbar.items = @[nextButton, prevButton, space, bar];
 toolbar.items = @[space, bar];
toolbar.items = @[space, bar, nextButton, prevButton];
    self.numTextField.inputAccessoryView = toolbar;





//使用 UITextFieldTextDidChangeNotification   监听textField.text只要改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];


//textField文字发生改变触发通知
-(void)textFieldDidChange:(NSNotification*)notification
{
   UITextField *textField = (UITextField *)[notification object];
}
remove通知
 [[NSNotificationCenter defaultCenter] removeObserver:self];

推荐阅读更多精彩内容