在UITextField和UITextField中能查到这两个属性

@property (readwrite, retain) UIView *inputView;

@property (readwrite, retain) UIView *inputAccessoryView;

在UITextField或者UITextField成为默认响应的时候,会弹出系统键盘。如果对这两个控件的inputView属性设置了自定义的view,在其成为第一响应的时候系统键盘将不再弹出,取而代之的是赋值给inputView的那个view。inputAccessoryView是键盘的辅助视图,即键盘上面那部分。同样当对inputAccessoryView设置了自定义view时,键盘弹出的同时,该view会作为辅助视图出现在键盘的上面,和键盘一起弹出。通常会使用pickerView作为自定义的弹出视图,这里为了简单起见,用一个普通的view演示:

基础代码演示

#import "ViewController.h"
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds@interface ViewController ()@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *customInputView;
@property (nonatomic, strong) UIToolbar *customAccessoryView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self loadBaseUI];
}- (void)loadBaseUI{[self.view addSubview:self.textField];
}- (UITextField *)textField{if (!_textField) {_textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, SCREEN_BOUNDS.size.width - 100, 30)];_textField.layer.borderWidth = 1.0;_textField.layer.borderColor = [UIColor lightGrayColor].CGColor;_textField.layer.cornerRadius = 4.0;_textField.placeholder = @"测试";_textField.inputView = self.customInputView;_textField.inputAccessoryView = self.customAccessoryView;}return _textField;
}- (UIView *)customInputView{if (!_customInputView) {_customInputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, 220)];_customInputView.backgroundColor = [UIColor lightGrayColor];UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_BOUNDS.size.width, 40)];label.textAlignment = NSTextAlignmentCenter;label.text = @"自定义inputView";[_customInputView addSubview:label];}return _customInputView;
}- (UIToolbar *)customAccessoryView{if (!_customAccessoryView) {_customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCREEN_BOUNDS.size.width,40}];_customAccessoryView.barTintColor = [UIColor orangeColor];UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];[_customAccessoryView setItems:@[space,space,finish]];}return _customAccessoryView;
}- (void)done{[self.textField resignFirstResponder];
}@end

点击textField后的显示效果

对其他控件设置inputView和inputAccessoryView

除了UITextField和UITextField,有事我们可能想让别的控件实现类似效果,比如点击一个按钮、选中某个cell的时候弹出键盘,但是对于UITextField和UITextField以外的控件,inputView和inputAccessoryView是只读的

@property (nonatomic, readonly, retain) UIView *inputView

@property (nonatomic, readonly, retain) UIView *inputAccessoryView

因此如果要使用控件这两个属性,就要创建该控件的子视图,然后重新申明inputView和inputAccessoryView为readwrite的,并重写它们的get方法,这样在UITableViewCell成为第一响应的时候会自动弹出inputView和inputAccessoryView。以UITableView演示:

代码示例

//MyTableViewCell.h#import <UIKit/UIKit.h>
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds@interface MyTableViewCell : UITableViewCell//属性申明为可读写
@property (nonatomic, strong, readwrite) UIView *inputView;@property (nonatomic, strong, readwrite) UIToolbar *inputAccessoryView;@end
//  MyTableViewCell.m#import "MyTableViewCell.h"@implementation MyTableViewCell//重写get方法
- (UIView *)inputView{if (!_inputView) {_inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, 220)];_inputView.backgroundColor = [UIColor lightGrayColor];UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_BOUNDS.size.width, 40)];label.textAlignment = NSTextAlignmentCenter;label.text = @"自定义inputView";[_inputView addSubview:label];}return _inputView;
}- (UIToolbar *)inputAccessoryView{if (!_inputAccessoryView) {_inputAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCREEN_BOUNDS.size.width,40}];_inputAccessoryView.barTintColor = [UIColor orangeColor];UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];[_inputAccessoryView setItems:@[space,space,finish]];}return _inputAccessoryView;
}- (void)done{[self resignFirstResponder];
}@end

在主视图控制器中创建一个tableView发现这样之后点击cell发现不会弹出inputView,最后查阅官方文档发现需要在子类中重写canBecomeFirstResponder方法:

在子类.m文件中添加

- (BOOL)canBecomeFirstResponder{return YES;
}

发现还是不行,最后手动becomeFirstResponder,终于实现了。PS原来选中都还不算是成为第一响应,坑爹啊!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{MyTableViewCell *cell = (MyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];[cell becomeFirstResponder];
}

效果图

原文链接点击打开链接

iOS开发inputView和inputAccessoryView相关推荐

  1. ios开发-inputView和inputAccessoryView

    说明 在UITextField和UITextField中能查到这两个属性 @property (readwrite, retain) UIView *inputView; @property (rea ...

  2. iOS开发UI篇—Date Picker和UITool Bar控件简单介绍

    iOS开发UI篇-Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...

  3. iOS开发之五:常用控件--UITextField的使用

    UITextField 是iOS开发中用的非常多的一种控件,主要是供用户输入单行信息的.下面来详细介绍UITextField. 1.常用属性 <span style="font-siz ...

  4. iOS开发中键盘样式和自定义键盘。

    文章目录 系统自带的样式 自定义键盘 在系统自带键盘基础上自定义键盘 完全自定义键盘 自定义全部类型键盘 系统自带的样式 在iOS开发中系统自带键盘已经有很多样式,但是有时候并不能满足我们都开发需求, ...

  5. IOS开发基础之微博项目第1天-OC版

    IOS开发基础之微博项目第1天-OC版 纯代码创建的项目,具有参考价值 该资料来自2014年7月3号,虽然时间过去较长,但是oc和swift不同,oc语法迭代更新慢 具有一定的参考意义 涉及xib加载 ...

  6. IOS开发Swift——开发小知识(持续更新)

    如有错误,请指正!谢谢! 侵权删!(部分转载) 1.PHAsset获取本地视频的url PHCachingImageManager().requestAVAsset(forVideo: asset, ...

  7. IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

    在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:    这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需 ...

  8. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇-transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  9. 【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示

    [ios开发/Xcode]使用UITableView完成学生信息及成绩的显示 设计思想 实现效果 源代码 设计思想 首先创建所有页面的故事版,包括,登录.注册与成绩页面 接着设置故事版的关联代码,如下 ...

最新文章

  1. .NET里面的Interop太烂了
  2. 机器学习视觉图像算法工程师--面试笔试--常考知识点乱找总结
  3. Linux之Redis安装
  4. XCTF-高手进阶区:PHP2
  5. mysqldump死住(实际是导致mysqld crash)
  6. 如何降低SQL语句复杂度
  7. 【转】云服务器cvm 云服务器ecs区别
  8. Ubuntu 无法获得锁
  9. dan.php,shenlingdan.php
  10. python之list[index1:index2]是左闭右开
  11. OSPF外部路由汇总
  12. 网格搜索、随机搜索和贝叶斯调参总结与python代码实践
  13. 力学现象演示——圆周运动与弹簧弹力
  14. 嵌入式、单片机和PLC哪个前景好一些?
  15. 佳博打印机驱动安装方法
  16. ASO优化:教你学会如何选词,技巧有哪些?
  17. 自定义拍照时 拍照界面_女研究生劝父亲盖房时把围墙退后三尺,新房成网红,一天20人拍照...
  18. HTTPs SSL OV、DV和EV证书的区别
  19. fir.im Weekly - 如何打造 Github 「爆款」开源项目
  20. 【计算机网络】—— 数据链路层的功能概述

热门文章

  1. OpenCV OAK-D-W广角相机测试
  2. 一些不错的嵌入式相关的公司
  3. 一篇文章概括——2021 不容错过的后端技术趋势(强烈推荐)
  4. 【matplotlib】 移动平均(Moving Average)
  5. 高老师的架构设计_隽语集(AA_0301)
  6. 团队的秘密 - 笔记
  7. 树莓派安装python opencv_树莓派4无痛安装OpenCV+python3
  8. 用 Serverless 快速搭建个人相册网站
  9. C语言和C++的不同之处和相同之处
  10. 【如何使用Medooze 实现多方视频会议】