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

 更新时间:2017年08月10日 10:47:18   作者:鸿鹄当高远  
大iOS中 键盘 KeyBoard 上怎么添加工具栏呢?大致思路是提前创建好工具栏,在键盘弹出的时候将工具栏显示出来,在键盘消失的时候让工具栏隐藏。具体实现代码大家参考下本文吧

 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生成与读取二维码名片的方法,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • iOS如何利用一句话完成转场动画

    iOS如何利用一句话完成转场动画

    这篇文章主要给大家介绍了关于iOS如何利用一句话完成转场动画的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • iOS实现动态自适应标签

    iOS实现动态自适应标签

    这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • iOS实现图片压缩的两种方法及图片压缩上传功能

    iOS实现图片压缩的两种方法及图片压缩上传功能

    ios 图片压缩有两种方法,分别是,压缩图片质量(Quality),压缩图片尺寸(Size),非常不错,具有参考借鉴价值,需要的朋友参考下
    2017-01-01
  • Objective-C中字符串NSString的常用操作方法总结

    Objective-C中字符串NSString的常用操作方法总结

    这篇文章主要介绍了Objective-C中字符串NSString的常用操作方法总结,Objective-C中NSString和NSMutableString这两个类下包含了操作字符串的大多数方法,需要的朋友可以参考下
    2016-04-04
  • iOS小数取整的方法(ceil floor round)示例

    iOS小数取整的方法(ceil floor round)示例

    这篇文章主要为大家介绍了iOS小数取整的方法(ceil floor round)示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • iOS实现图片轮播器

    iOS实现图片轮播器

    这篇文章主要为大家详细介绍了iOS如何实现图片轮播器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS 和 Android 哪个更利于赚钱?

    iOS 和 Android 哪个更利于赚钱?

    iOS 和 Android 哪个更利于赚钱?这篇文章为大家揭晓答案,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • iOS视频编辑之添加音轨的方法

    iOS视频编辑之添加音轨的方法

    本篇文章主要介绍了iOS视频编辑之添加音轨的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • IOS展开三级列表效果示例

    IOS展开三级列表效果示例

    今天介绍的是一个很不错的三级列表展开效果的例子,文章运用实例代码介绍的很详细,提供给学习IOS的小伙伴们使用。
    2016-08-08

最新评论