如何textview收起键盘 view 怎么收键盘

- (void)viewDidLoad
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
// Do any additional setup after loading the view.
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 40, 240, 40)];
textField.borderStyle = UITextBorderStyleRoundedR
textField.placeholder = @&请输入:&;
textField.clearButtonMode = UITextFieldViewModeA
textField.keyboardType = UIKeyboardTypeNumberP
textField.returnKeyType = UIReturnKeyD
//自定义 可以看出来自定义键盘只与高度有关 其他不影响自定义键盘的位置大小
UIView * keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 1, 100)];
keyboardView.backgroundColor = [UIColor redColor];
//把view赋值给inputView
textField.inputView = keyboardV
//设置二级键盘
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 320, 20)];
label.text = @&男&;
label.backgroundColor = [UIColor grayColor];
textField.inputAccessoryView =
[self.view addSubview:textField];
版权声明:本文为博主原创文章,未经博主允许不得转载。当前访客身份:游客 [
为API生,为框架死,为Debug奋斗一辈子;吃符号的亏,上大小写的当,最后死在需求上。微博 ID:芳仔小脚印
:楼主你好,我自己写了一个类,用代理实现了接收消...
:很详细,很有帮助,感谢。
:你快回来~
:引用来自“blue1990”的评论你好,我在http://de...
:引用来自“blue1990”的评论你好,我在http://de...
:引用来自“carrylongc”的评论 通讯录中电话号码...
:通讯录中电话号码中的"-"怎么删除啊 求知道 qq 3...
:引用来自“陈茵”的评论楼主,你这个单例封装好该...
今日访问:1124
昨日访问:1315
本周访问:6366
本月访问:5076
所有访问:505582
iOS学习4:UITextView的用法及技巧(ios7下光标bug解决方案)
发表于2年前( 13:22)&&
阅读(26191)&|&评论()
0人收藏此文章,
textView的创建;设置属性;代理方法;隐藏键盘;键盘弹出时调整textView的位置,像手机短信的效果
一、新建一个textView
UITextView *textView = [[[UITextView alloc] init] autorelease];
//设置代理 需在interface中声明UITextViewDelegate
textView.delegate =
//字体大小
textView.font = [UIFont systemFontOfSize:16];
//添加滚动区域
textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);
//是否可以滚动
textView.scrollEnabled = NO;
//获得焦点
[textView becomeFirstResponder];
[self.view addSubview:textView];
二、键盘操作
//返回键的类型
textView.returnKeyType = UIReturnKeyD
//键盘类型
textView.keyboardType = UIKeyboardTypeD
三、隐藏键盘的几种方式
个人还是认为最方便的是在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘
①在键盘上加上隐藏按钮
//定义一个toolBar
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
//设置style
[topView setBarStyle:UIBarStyleBlack];
//定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
UIBarButtonItem * button1 =[[UIBarButtonItem
alloc]initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem * button2 = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//定义完成按钮
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone
target:self action:@selector(resignKeyboard)];
//在toolBar上加上这些按钮
NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
[topView setItems:buttonsArray];
[textView setInputAccessoryView:topView];
//隐藏键盘
- (void)resignKeyboard {
[textView resignFirstResponder];
} 最终效果
还有几种也可隐藏键盘的方式
②用回车键,前提是你的textView中不需要用到回车键
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"])
[textView resignFirstResponder]; return NO;
return YES;
③触摸空白处隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//隐藏键盘
[textView resignFirstResponder];
四、使键盘不挡住输入框
& & 在view中添加一个子view,设置此子view的tag值为1000,在此view上添加一个textView和一个发送按钮,如下图;我们要达到textView的键盘弹出时,整个View往上平移,键盘消失,view往下平移的效果,模拟发送短信的界面。
设置textView圆角
//设置textView圆角
[self.textView.layer setCornerRadius:10];
①、在viewWillAppear中添加键盘监听事件
//添加键盘的监听事件
//注册通知,监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil]; ②、完成①selector中键盘弹出keyboardDidShow:和消失keyboardDidHidden方法
& & 在.m文件#import后面添加
//动画时间
#define kAnimationDuration 0.2
//view高度
#define kViewHeight 56
// 键盘弹出时
-(void)keyboardDidShow:(NSNotification *)notification
//获取键盘高度
NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardR
[keyboardObject getValue:&keyboardRect];
//调整放置有textView的view的位置
//设置动画
[UIView beginAnimations:nil context:nil];
//定义动画时间
[UIView setAnimationDuration:kAnimationDuration];
//设置view的frame,往上平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)];
[UIView commitAnimations];
//键盘消失时
-(void)keyboardDidHidden
//定义动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kAnimationDuration];
//设置view的frame,往下平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)];
[UIView commitAnimations];
iOS7光标问题
PS:有网友遇到textView在ios7上出现编辑进入最后一行时光标消失,看不到最后一行,变成盲打,stackOverFlow网站上有大神指出,是ios7本身bug,加上下面一段代码即可(网友调试得出,在此mark一下,有问题,欢迎大神们指出)
-(void)textViewDidChange:(UITextView *)textView {
CGRect line = [textView caretRectForPosition:
textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height
- ( textView.contentOffset.y + textView.bounds.size.height
- textView.contentInset.bottom - textView.contentInset.top );
if ( overflow & 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = textView.contentO
offset.y += overflow + 7; // leave 7 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[UIView animateWithDuration:.2 animations:^{
[textView setContentOffset:offset];
更多开发者职位上
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage推荐这篇日记的豆列
······1、通过在textView中键盘上方添加一个toolBar来实现取消键盘的功能,可以添加其他功能:
1 UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = @[btnSpace, doneButton];
[topView setItems:buttonsArray];
[_textView setInputAccessoryView:topView];
2、如果上方有一个navigationBar,则可以在bar上添加一个DONE按钮来取消键盘,如果bar上已经有其他功能按键,则可以弹出键盘前保存当前barButtonItem,然后在弹出键盘(利用shouldBeginEditing委托来触发)后用取消键盘按钮来替换:
1 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
self.rightButton = self.navigationItem.rightBarButtonI
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
self.navigationItem.rightBarButtonItem = doneB
return YES;
键盘消失后再赋回原来的按钮:
- (void)dismissKeyBoard
[_textView resignFirstResponder];
self.navigationItem.rightBarButtonItem = self.rightB
阅读(...) 评论()主题 : 如何text view 怎么收键盘?就是那种点下旁边空白处,键盘就收起来了.
级别: 新手上路
UID: 307548
可可豆: 97 CB
威望: 60 点
在线时间: 47(时)
发自: Web Page
来源于&&分类
如何text view 怎么收键盘?就是那种点下旁边空白处,键盘就收起来了.&&&
问个事,我用图形化 UI, 拖入了一个text view 写了段东西. 碰了文章的内容后会弹出键盘,但点旁边空白处,没办法去掉.
级别: 新手上路
UID: 316729
可可豆: 269 CB
威望: 181 点
在线时间: 136(时)
发自: Web Page
你return怎么设置的?不都是第一响应吗- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{&&&&[xxxxx&&resignFirstResponder];&&&&[xxxxx&&resignFirstResponder];&&&&[xxxxx&&resignFirstResponder];}另外告诉你和 resignFirstResponder对应的becomeFirstResponder
有一个朋友说我帅气的很,这哥们忒爱说真话,没办法,人家就是有眼光
级别: 侠客
可可豆: 255 CB
威望: 133 点
在线时间: 283(时)
发自: Web Page
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{&&&&&&&&if (![self.buyPrice isExclusiveTouch] | ![self.sellPrice isExclusiveTouch] | ![self.count isExclusiveTouch] ) {&&&&&&&&[self.buyPrice resignFirstResponder];&&&&&&&&[self.sellPrice resignFirstResponder];&&&&&&&&[self.count resignFirstResponder];&&&&}}
级别: 新手上路
可可豆: 17 CB
威望: 17 点
在线时间: 21(时)
发自: Web Page
主要是resignFirstResponder&& 意思就是失去第一响应者&&自然键盘就回收了
级别: 新手上路
UID: 322737
可可豆: 109 CB
威望: 78 点
在线时间: 81(时)
发自: Web Page
写一个xib,上面定义一个按钮,使文本框失去第一响应者。&&再放到键盘上方
级别: 新手上路
可可豆: 212 CB
威望: 162 点
在线时间: 527(时)
发自: Web Page
//添加触摸手势,当点击View上其他地方时会隐藏键盘&&&&UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignResponders)];&&&&//将触摸事件添加到当前view&&&&[self.view addGestureRecognizer:tap];//点击View任意空白处,关闭键盘- (void)resignResponders {&&&&&&&&[text view resignFirstResponder];}
关注本帖(如果有新回复会站内信通知您)
3*3+1 正确答案:10
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版}

我要回帖

更多关于 ios textview隐藏键盘 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信