1、简介

  起初看到这两个属性是在UIResponder中,只是可读的:

@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputView NS_AVAILABLE_IOS(3_2);
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);

  后来在UITextField和UITextView中也有,是可读可写的:

@property (nullable, readwrite, strong) UIView *inputView;
@property (nullable, readwrite, strong) UIView *inputAccessoryView;

  用一张输入法的图片简单说明一下这两个属性:

  inputAccessoryView:附件视图,就是上面汉子和拼音

  inputView:就是下面的按键输入法

  最上面的输入框:之前作法——监听键盘的通知,获取键盘高度处理输入框位置;

          现在——是否可以把输入框放入到附件视图inputAccessoryView?只是一种想法,后期我会自己试试 勿喷!

2、自定义inputAccessoryView和inputView

  2.1、直接赋值UITextField的这两个属性

    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(, , , )];
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(endEdi:)];
UIBarButtonItem *right2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(endEdi:)];
toolBar.items = [NSArray arrayWithObjects:right,right2,right,right,right,right,right, nil];
self.inputTextField.inputAccessoryView = toolBar;
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
UIImageView *igView = [[UIImageView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
igView.image = [UIImage imageNamed:@"pic2.png"];
[inputView addSubview:igView];
inputView.backgroundColor = [UIColor greenColor];
self.inputTextField.inputView =inputView;

  • 设置坐标时只有高度才能起到约束,x、y、weight设置也不会有作用;
  • 整个弹出视图的高度是inputView.height+inputAccessoryView.height,而且附件视图会紧贴inputView视图上面;
  • inputView和inputAccessoryView的子视图需要有详细坐标约束;
  • UIToolbar继承UIView,常用于附件视图上,作点击事件处理;

  2.2、自定义控件的这两个属性

    往往会用这两个属性来自定义键盘,其实可以自定义许多底部的弹出框!

    由于UIResponder有这两个属性,所有大多数视图可以重写这个两个属性,以UILabel为例:

@interface MenuLabel()<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIToolbar *_inputAccessoryView;
UIPickerView *_inputView;
}
@end
@implementation MenuLabel - (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setUI];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
self.multipleTouchEnabled = YES;
[self setUI];
}
- (void)setUI{
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
[self addGestureRecognizer:tap];
} - (void)tapGes:(UITapGestureRecognizer *)ges{
[self becomeFirstResponder];
}
-(UIView *)inputAccessoryView{
if(!_inputAccessoryView)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view.backgroundColor = [UIColor grayColor]; UIToolbar *toolBar = [[UIToolbar alloc]init];
toolBar.frame = CGRectMake(, , , );
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dodo)];
toolBar.items = [NSArray arrayWithObject:right]; [view addSubview:toolBar];
return view;
}
return _inputAccessoryView;
}
-(UIPickerView *)inputView{
if(!_inputView)
{
UIPickerView * pickView = [[UIPickerView alloc]init];
pickView.delegate =self;
pickView.dataSource = self;
pickView.showsSelectionIndicator = YES;
return pickView;
}
return _inputView;
}
-(void)dodo{
[self resignFirstResponder];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return ;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [NSString stringWithFormat:@"%ld",row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return ;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.text =[NSString stringWithFormat:@"%ld",row];
} - (BOOL)canBecomeFirstResponder{
return YES;
}
@end

  • 要设置  self.userInteractionEnabled = YES;重写- (BOOL)canBecomeFirstResponder{     return YES; } 方法;
  • 这种方法附件的高度可以修改,但是底部并不会随inputView高度改变而变化,是固定的;
  • inputView不设置坐标,会自适应展示,由于系统inputView高度固定,也不知道不同版本会不会变化,所以就不设置自定义视图inputView的高度 过高会覆盖附件,过低会覆盖不全;
  • 附件的子视图需要详细坐标约束;

iOS开发inputView和inputAccessoryView的更多相关文章

  1. iOS开发小技巧--iOS键盘 inputView 和 inputAccessoryView

    iOS键盘 inputView 和 inputAccessoryView 1.inputAccessoryView UITextFields和UITextViews有一个inputAccessoryV ...

  2. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  3. iOS开发之微信聊天工具栏的封装

    之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...

  4. iOS开发之自定义表情键盘(组件封装与自动布局)

    下面的东西是编写自定义的表情键盘,话不多说,开门见山吧!下面主要用到的知识有MVC, iOS开发中的自动布局,自定义组件的封装与使用,Block回调,CoreData的使用.有的小伙伴可能会问写一个自 ...

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

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

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

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

  7. iOS开发之新浪微博山寨版代码优化

    之前发表过一篇博客“IOS开发之新浪围脖”,在编写代码的时候太偏重功能的实现了,写完基本功能后看着代码有些别扭,特别是用到的四种cell的类,重复代码有点多,所以今天花点时间把代码重构一下.为了减少代 ...

  8. 【Swift】iOS开发历险记(二)

    前言 这个系列主要是一些开发中遇到的坑记录分享,有助于初学者跨过这些坑,攒够 7 条发一篇. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯 ...

  9. 让你快速了解并掌握如何进行iOS开发技能

    首先你要花点时间针对objective-c语言的学习:毕竟这个是iOS开发的基础(你也可以尝试用Swift,但此项目只是针对OC),编程套路其实都是差不多,多写多想多实践:关于环境的搭建就不在本文进行 ...

随机推荐

  1. Ruby——输入&输出

    Ruby的输入和输出操作.输入是程序从键盘.文件或者其他程序读取数据.输出是程序产生数据.可以输出到屏幕.文件或者其他程序. Ruby中的一些类有些方法会执行输入&输出操作.例如Kernel. ...

  2. Eclipse中properties文件,中文只显示Unicode问题(Properties Editor)

    我们常常在properties文件中添加中文注释,而properties文件的中文需用unicode表示, 使用eclipse默认的properties文件编辑器查看显示中文为乱码. 即便修改prop ...

  3. NX二次开发-Block UI C++界面(表达式)控件的获取(持续补充)

    Expression(表达式)控件的获取 NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); // ...

  4. 【系统安全性】四、认证Authentication

    四.认证Authentication 1.为什么要认证 对请求.数据进行认证,判断伪造的数据 HTTP请求很脆弱,抓包软件很强大,容易伪造身份,非法获取数据 2.摘要认证 对象:客户端参数.服务端响应 ...

  5. JS对象的引用,对象的拷贝

    目录 一.场景 二.浅拷贝 三.深拷贝 一.场景 除了基本类型跟null,对象之间的赋值,只是将地址指向同一个,而不是真正意义上的拷贝 将一个对象赋值给另外一个对象. var a = [1,2,3]; ...

  6. 22、继续javascript,左边选中的跳到右边

    1. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...

  7. socket模拟通信

    import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...

  8. 如何在Python中让两个print()函数的输出打印在一行内?

    1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认末尾加 ...

  9. 1.1 React 介绍

    1.1.1 React 是什么 React IS A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES 来自:React 官方网站 狭义来讲 React ...

  10. 洛谷 P3803 【模板】多项式乘法(FFT)

    题目链接:P3803 [模板]多项式乘法(FFT) 题意 给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(F(x)\) 和 \(G(x)\) ...