iOS–inputView和inputAccessoryView


什么是inputView和inputAccessoryView?

如果是UITextField和UITextView,下面是声明文件源代码:

// set while first responder, will not take effect until reloadInputViews is called.
@property (readwrite, retain) UIView *inputView;
@property (readwrite, retain) UIView *inputAccessoryView;

如果是其他的控件,继承了UIResponder,(UIView 是UIResponder的子类),下面是声明文件源代码:

// Called and presented when object becomes first responder.  Goes up the responder chain.
@property (nonatomic, readonly, retain) UIView *inputView NS_AVAILABLE_IOS(3_2);
@property (nonatomic, readonly, retain) UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);

自定义TextField或TextView

自定义只需要重写下面的两个属性

textField.inputView = [UIView alloc] init];
textField.inputAccessoryView = [UIToolbar alloc] init];

自定义继承了UIResponder类的控件

需要自定义UIResponder的子类,我们需要在重新定义一个子类,然后再这个子类中声明inputViewinputAccessoryView为可读可写属性。然后重写getter方法。同时还需要覆盖-(BOOL)canBecomeFirstResponder方法。

下面以UIButton为例子的子类重设代码:

#import <UIKit/UIKit.h>

@interface ABEButton : UIButton

@property (nonatomic, strong)UIToolbar *inputAccessoryView;
@property (nonatomic, strong)UIView *inputView; @end
#import "ABEButton.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@implementation ABEButton

-(BOOL)canBecomeFirstResponder
{
return YES;
} #pragma mark- Getter, Setter
- (UIView*)inputView{
if (!_inputView) {
_inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 220)];
_inputView.backgroundColor = [UIColor redColor];
}
return _inputView;
} - (UIToolbar*)inputAccessoryView{
if (!_inputAccessoryView) {
_inputAccessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
_inputAccessoryView.backgroundColor = [UIColor grayColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"隐藏" style:UIBarButtonItemStylePlain target:self action:@selector(clickFinishButton:)];
_inputAccessoryView.items = @[item];
}
return _inputAccessoryView;
} #pragma mark- Private method
- (void)clickFinishButton:(ABEButton*)buttonItem{
[self resignFirstResponder];
}
@end

这样,我们就可以使用点击button,弹出inputView了。


注意:不要忘记为button添加becomeFirstResponder。

iOS--inputView和inputAccessoryView的更多相关文章

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

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

  2. iOS开发inputView和inputAccessoryView

    1.简介 起初看到这两个属性是在UIResponder中,只是可读的: @property (nullable, nonatomic, readonly, strong) __kindof UIVie ...

  3. 【iOS发展-70】点菜系统案例:使用文本框inputView和inputAccessoryView串联UIPickerView、UIDatePicker和UIToolBar

    (1)效果 (2)先在storyboard中设计界面,然后源码(直接在ViewController中码) #import "ViewController.h" @interface ...

  4. inputAccessoryView,inputView

    我们在使用UITextView和UITextField的时候,可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图,通常是UIToolBar,用于回收键盘. 但是当我 ...

  5. iOS 7 隐藏特性

    当 iOS7 刚发布的时候,全世界的苹果开发人员都立马尝试着去编译他们的app,接着再花上数月的时间来修复任何出现的故障,甚至重做app.这样的结果,使得人们根本无暇去探究 iOS7 所带来的新东西. ...

  6. ios之键盘的自定义

    一.键盘通知 当文本View(如UITextField,UITextView,UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstresp ...

  7. 对于iOS 7 隐藏特性和解决之道

    当 iOS7 刚发布的时候,全世界的苹果开发人员都立马尝试着去编译他们的app,接着再花上数月的时间来修复任何出现的故障,甚至重做app.这样的结果,使得人们根本无暇去探究 iOS7 所带来的新东西. ...

  8. UIResponder

    原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...

  9. 你真的了解UITextField吗?

    一:首先查看一下关于UITextField的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextIn ...

随机推荐

  1. Python 类型

    文章出处:http://www.cnblogs.com/winstic/,请保留此连接   python是动态类型语言,不需要预先声明变量的类型,变量类型和值在赋值的那一刻被初始化   python使 ...

  2. jackson学习----解析豆瓣的图书信息

      异常一. org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple ...

  3. MongoDB update数据语法【转】

    在前面的文章“mongodb 查询的语法”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方 ...

  4. C语言头文件的作用

    C99中规定, 所有顶层的默认存储类标志符都是extern ! 头文件中声明的函数,默认都是extern前缀.但是为了我们程序员方便,我们采取下面的手段: 我个人认为是extern催生了头文件的诞生. ...

  5. hadoop-2.0.0-cdh4.2.1源码编译总结

    经过一个星期多的努力,这两个包的编译工作总算告一段落. 首先看一下这一篇文章: 在eclipse下编译hadoop2.0源码 http://www.cnblogs.com/meibenjin/arch ...

  6. MyEclipse 设置字体

    过了好几年,又开始用myeclipse  都忘了,  这里贴图用于记录 1.打开window - preferences . 2.打开General-Appearance-Colors and Fon ...

  7. HDOJ/HDU 1865 1sting(斐波拉契+大数~)

    Problem Description You will be given a string which only contains '1'; You can merge two adjacent ' ...

  8. 《A First Course in Mathematical Modeling》-chaper2-建模过程、比例性及几何相似性

    这一章节着重从整体的层面给出数学建模过程中一个泛式流程,它给出了在给现实模型建立数学模型的框架性思路,但是需要注意的是,虽然这里称其为一种“泛式”思路,但是在具体的问题的分析中,整个建模过程还是充满了 ...

  9. weekend110(Hadoop)的 第六天笔记

    (2015年1月25日) 课程目录 01-复习ha相关 02-hive的元数据库mysql方式安装配置 03-hive的使用 04-hive的常用语法 05-hql语法及自定义函数 06-hbase表 ...

  10. 挣值管理不是搞数字游戏(3)——进阶指标:CV、SV、CPI、SPI、EAC

    摘要: 要考PMP(Project Management Professional ),挣值管理是必考的知识.软件项目有很大的特殊性,不少人认为挣值管理不太适用于软件项目.挣值管理相关资料也比较超多, ...