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. 我的PHP之旅--XML操作

    XML操作 XML主要是做数据存储和WEB服务的,所以我们难免要操作它,这里只介绍PHP的simpleXML方式. 我们要操作的XML: <?xml version="1.0" ...

  2. Hidden Password

    zoj1729:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 题意:就是求字符串的最小表示,模板题. 题解:直接贴模板. ...

  3. codeforces C. Jzzhu and Chocolate

    http://codeforces.com/contest/450/problem/C 题意:一个n×m的矩形,然后可以通过横着切竖着切,求切完k次之后最小矩形面积的最大值. 思路:设k1为横着切的次 ...

  4. 【LA2796】Concert Hall Scheduling(最大费用最大流)

    Description You are appointed director of a famous concert hall, to save it from bankruptcy. The hal ...

  5. 使用mysql-proxy代理实现msyql数据库读写分离

    要实现读写分离,可以先看看如何实现mysql数据库主从:http://www.cnblogs.com/sustudy/p/4174189.html mysql-proxy下载地址(要看好对应版本):h ...

  6. [SQL Server]一次执行资料夹内的.sql 指令码

    原文:[SQL Server]一次执行资料夹内的.sql 指令码 初始资料库时,我们Developers们会准备很多.sql指令码来建立资料表.检视甚至初始资料,那麽要怎麽一次执行资料夹内的*.sql ...

  7. 集成activiti-modeler 到 自己的业务系统

    本文目的: 将activit 5.12.1 的 modeler 流程设计器 集成到自己的工程中去 解决问题: 1. 复制相关资源文件到自己的工程中 2. 解决modeler的路径访问问题,迁移到非系统 ...

  8. Android数据存储之文件存储

    首先给大家介绍使用文件如何对数据进行存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的. public ...

  9. eclipse 中使用等宽字体 inconsolata

    一直以来,就感觉使用 eclipse 时的那几种字体很难看,而且非等宽,空格宽度很小,排版很乱. 搜索并试用了一下,发现了字体inconsolata. 这是一个很适合编程的字体,效果如下: 非常漂亮. ...

  10. (转载)prepare函数的学习,我要学习php第二天

    (转载)http://www.boyuan78.com/htm/company/2012_1030_60.html prepare函数的学习,我要学习php第二天 $mysqli = new mysq ...