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

IOS 关于键盘遮挡编辑区域(UITextFiled/UITextView)的问题

时间:2016-01-15 14:40:45      阅读:706      评论:0      收藏:0      [点我收藏+]

标签:

  参考自:http://blog.csdn.net/windkisshao/article/details/21398521

1.自定方法 ,用于移动视图

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval;

2.注册监听

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    [defaultCenter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

3.实现方法

- (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];

    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    if(nil==self.myTextView) return;//    self.editTextView 为被键盘遮挡住的控件

    CGRect rect = self.myTextView.frame;

    float textY = rect.origin.y + rect.size.height; 

    float bottomY = SCREENHEIGHT - textY;//得到下边框到底部的距离  SCREENHEIGHT 为当前设备的高度

    if(bottomY >=keyboardRect.size.height ){//键盘默认高度,如果大于此高度,则直接返回

        return;

    }

    float moveY = keyboardRect.size.height - bottomY;

    [self moveInputBarWithKeyboardHeight:moveY withDuration:animationDuration];

}

 

- (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];

}

 

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval{

  CGRect rect1 = self.view.frame;

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:_NSTimeInterval];

    rect1.origin.y = -_CGRectHeight;//view往上移动

    self.view.frame = rect1;

    [UIView commitAnimations];

}

测试的时候是用的4s真机测试的,一切都很完美,但是前几天同事用6 测的时候发现一个问题,就是编辑的时候能达到不遮挡控件的效果,但是发现输入中文的时候有时候会莫名的敲不出中文,会出现一连串的字符,而且删除的时候字符删不掉,几乎是越删越多.但是用4S测了很多遍都没有出现这个问题.这几天一直在想这个问题是什么造成的,望知道的高人们,指点一二.感谢!(用的UITextView)

技术分享

IOS 关于键盘遮挡编辑区域(UITextFiled/UITextView)的问题

标签:

原文地址:http://www.cnblogs.com/Cyan-zoey/p/5133167.html

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