码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 监听键盘高度,输入框上升

时间:2015-07-15 22:40:50      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

 1 //设置输入框 ---《因为输入框用了get方法,所以第一次调用输入框要用self 调用》;
2 self.textlab.frame=CGRectMake(20, 420, 250, 30); 3 _textlab.layer.borderColor=[UIColor blueColor].CGColor; 4 _textlab.layer.borderWidth= 0.5f; 5 _textlab.backgroundColor=[UIColor colorWithRed:0.830 green:0.801 blue:0.881 alpha:1.000]; 6 7 //添加输入框到视图 8 [self.view addSubview:self.textlab]; 9 10 //监听键盘,键盘出现 11 [[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(keyboardwill:)
name:UIKeyboardWillShowNotification object:nil]; 12 13 //监听键盘隐藏 14 [[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(keybaordhide:)
name:UIKeyboardWillHideNotification object:nil]; 15 16 //设置点击手势,当点击空白处,结束编辑,收回键盘 17 UITapGestureRecognizer *tapp=[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapAction:)];
18 //开启交互 19 self.view.userInteractionEnabled=YES; 20 //添加手势到视图 21 [self.view addGestureRecognizer:tapp];

 2、里面方法的实现

 1 //点击手势方法
 2 -(void)tapAction:(UITapGestureRecognizer *)sender
 3 {
 4     [self.view endEditing:YES];
 5 }
 6 
 7 //当键盘出现时,调用此方法
 8 -(void)keyboardwill:(NSNotification *)sender
 9 {
10     //获取键盘高度
11     NSDictionary *dict=[sender userInfo];
12     NSValue *value=[dict objectForKey:UIKeyboardFrameEndUserInfoKey];
13     CGRect keyboardrect = [value CGRectValue];
14     int height=keyboardrect.size.height;
15     
16     //如果输入框的高度低于键盘出现后的高度,视图就上升;
17     if ((_textlab.frame.size.height + _textlab.frame.origin.y)>(self.view.frame.size.height - height))
18     {
19         self.view.frame = CGRectMake(0, -height, self.view.frame.size.width, self.view.frame.size.height);
20     }
21 }
22 
23 //当键盘隐藏时候,视图回到原定
24 -(void)keybaordhide:(NSNotification *)sender
25 {
26      self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
27 }
28 
29 //输入框的get方法
30 -(UITextField *)textlab
31 {
32     if (_textlab==nil)
33     {
34         _textlab=[UITextField new];
35     }
36     return _textlab;
37 }

 防止键盘遮挡方式二;

调用UITextField 输入框的代理方法 <UITextFieldDelegate>

 1 //开始编辑输入框的时候,软键盘出现,执行此事件
 2 -(void)textFieldDidBeginEditing:(UITextField *)textlab
 3 {
 4     //获取当前输入框的位置信息
 5     CGRect frame = textlab.frame;
 6     //获取键盘底部位置与键盘最高点之间的距离,键盘高度256
 7     int offset = frame.origin.y + frame.size.height - (self.view.frame.size.height - 256.0);
 8     //设置视图上升的时间
 9     NSTimeInterval animaTime = 0.30f;
10     //字符串随意也行...
11     [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
12     //添加视图上的上升时间0.3f
13     [UIView setAnimationDuration:animaTime];
14     
15     //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
16     if(offset > 0)
17         self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
18     //提交动画
19     [UIView commitAnimations];
20 }
//当用户按下return键或者按回车键,keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //收回键盘第一响应者意思...
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

 

iOS 监听键盘高度,输入框上升

标签:

原文地址:http://www.cnblogs.com/qq95230/p/4649623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!