iOS中键盘 KeyBoard 上添加工具栏的方法

2020-01-21 00:02:08刘景俊

实现键盘通知的方法


#pragma mark 当键盘出现或改变时调用 
- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
 //键盘弹出时显示工具栏 
 //获取键盘的高度 
 NSDictionary *userInfo = [aNotification userInfo]; 
 NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
 CGRect keyboardRect = [aValue CGRectValue]; 
 float keyBoardHeight = keyboardRect.size.height; 
 // NSLog(@"%ld",(long)keyBoardHeight); 
 [UIView animateWithDuration:0.1 animations:^{ 
  _toolView.frame = CGRectMake(0, screen_Height-keyBoardHeight-50, screen_Width, 50); 
 }]; 
} 
#pragma mark 当键退出时调用 
- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
 //键盘消失时 隐藏工具栏 
 [UIView animateWithDuration:0.1 animations:^{ 
  _toolView.frame = CGRectMake(0, screen_Height+50, screen_Width, 50); 
 }]; 
} 

给工具栏上的各个按钮实现点击事件


- (void)btnClick{ 
 [textField resignFirstResponder]; 
} 
- (void)imageBtnClick{ 
} 
- (void)cameraBtnClick{ 
} 
- (void)canclebtnBtnClick{ 
} 

PS:下面看下iOS 键盘上方增加工具栏的代码。

具体代码如下所示:


UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                style:UIBarButtonItemStyleBordered target:self
                action:@selector(doneClicked:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
txtField.inputAccessoryView = keyboardDoneButtonView;

 


注:相关教程知识阅读请移步到IOS开发频道。