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. java EE学习流程(第二版更新)

    这周有点堕落了,这两天啥都没写,就顾上刷<庆余年>了

  2. css样式总结体会

    css属性值语法:https://developer.mozilla.org/zh-CN/docs/Web/CSS/Value_definition_syntax 1.margin-top属性不起作用 ...

  3. Thymeleaf 模板布局三种区别

  4. DGIM算法

    /***************************************************** copyright (C), 2014-2015, Lighting Studio. Co ...

  5. asp.net core网关Ocelot的简单介绍& Ocelot集成Identity认证

    文章简介  Ocelot网关简介 Ocelot集成Idnetity认证处理 Ocelot网关简介 Ocelot是一个基于netcore实现的API网关,本质是一组按特定顺序排列的中间件.Ocelot内 ...

  6. Haskell语法

    http://www.ibm.com/developerworks/cn/java/j-cb07186.html 1. 构造符号 : 比如: 1:2:3:[] 而常用的 [1,2,3] 是一种语法糖( ...

  7. Codeforces 1189A Keanu Reeves

    题目链接:http://codeforces.com/problemset/problem/1189/A 思路:统计1 和 0 的个数,不相等拆开字符串,否则不拆. AC代码: #include< ...

  8. 前端(十四)—— JavaScript常用类:Number、Date类、字符串、数组、Math类、正则

    JS常用类:Number类.Date类.Math类.字符串.数组.正则 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | - ...

  9. [已解决]报错UnicodeDecodeError

    输出报错: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 220: in 解决方案:将编码方式utf-8 修 ...

  10. 如果在vue中实现一个输入框的抖动效果?

    1. 先来理下思路? 1)抖动就是摆动,现实中的钟摆可以很形象. 2)当摆动到临界点后,就会向相反的方向摆动. 3)在没有动力时,摆动会慢慢停止. 2.用法: :start.sync 里面是抖动器名字 ...