iOS中键盘 KeyBoard 上添加工具栏的方法
2017-08-10 10:47
447 查看
iOS中 键盘 KeyBoard 上怎么添加工具栏?
如图中所示 在键盘上面加一条工具栏
大致思路是提前创建好工具栏,在键盘弹出的时候将工具栏显示出来,在键盘消失的时候让工具栏隐藏
上代码
设置两个变量
UIView * _toolView; //工具栏 UITextField *textField;// 输入框 呼出键盘用
创建工具栏 输入框 添加键盘弹出 消失的通知
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 64, 120, 60)]; textField.placeholder = @"测试"; [self.view addSubview:textField]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; //初始化工具栏 _toolView = [[UIView alloc]init]; _toolView.frame = CGRectMake(0, screen_Height, screen_Width, 50); [self.view addSubview:_toolView]; UIButton *losebtn = [UIButton buttonWithType:UIButtonTypeCustom]; losebtn.frame = CGRectMake(20, 0, 50, 50); [losebtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [losebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [losebtn setTitle:@"收起" forState:UIControlStateNormal]; [_toolView addSubview:losebtn]; UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [imageBtn setTitle:@"图片" forState:UIControlStateNormal]; imageBtn.frame = CGRectMake(screen_Width-100, 0, 50, 50); [imageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [imageBtn addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_toolView addSubview:imageBtn]; UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [cameraBtn setTitle:@"相机" forState:UIControlStateNormal]; cameraBtn.frame = CGRectMake(screen_Width-50, 0, 50, 50); [cameraBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [cameraBtn addTarget:self action:@selector(cameraBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_toolView addSubview:cameraBtn]; UIButton *canclebtn = [UIButton buttonWithType:UIButtonTypeCustom]; [canclebtn setTitle:@"取消" forState:UIControlStateNormal]; canclebtn.frame = CGRectMake(screen_Width-150, 0, 50, 50); [canclebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [canclebtn addTarget:self action:@selector(canclebtnBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_toolView addSubview:canclebtn]; }
实现键盘通知的方法
#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所有版本适用
- iOS 键盘添加工具栏
- IOS数字键盘左下角添加完成按钮的实现方法
- iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
- iOS键盘弹出的时候会挡住底部的控件,可以通过以下方法将整个UIView上移。
- 第2月第6天 iOS 运行时添加属性和方法
- 键盘工具栏的快速集成--IQKeyboardManager
- ios动态添加属性的几种方法
- IOS 点击空白处隐藏键盘的几种方法
- iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
- iOS_Runtime修改变量值,交换方法实现,动态添加类,成员变量和方法
- iOS开发中使用cocos2d添加触摸事件的方法
- IOS关闭键盘的方法
- IOS 在MkMapView中添加自己当前位置的大头针的方法
- 蓝牙键盘按键添加方法
- vs2008中将开始执行按钮(不调试按钮)添加至标准工具栏方法
- win7虚拟机iOS坑爹法语键盘冲突解决方法
- IOS解决键盘挡住UITextView的方法
- ios 处理键盘回收最全的方法
- IOS开发UI篇之──键盘添加工具条