textView、textField键盘遮挡

最近在做一个文本录入的app,多处需要处理键盘遮挡的问题,在这里对用到的方法进行整理

1.单个文本框输入遮挡,可以通过键盘通知方法里进行计算移动视图

- (void)viewDidLoad { } 方法里添加键盘监听

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(changeContentViewPosition:)

name:UIKeyboardWillShowNotification

object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(backViewPosition:)

name:UIKeyboardWillHideNotification

object:nil];

键盘显示和键盘收回的方法里做如下处理

- (void) changeContentViewPosition:(NSNotification *)notification

{

NSDictionary *info = [notification userInfo];

CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

CGRect frame = pwView.frame;

int offset = frame.origin.y + 50 - (self.view.frame.size.height -  keyboardSize.height);

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

if(offset > 0)

self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);

[UIView commitAnimations];

}


-(void)backViewPosition:(NSNotification *)notification

{

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

[UIView commitAnimations];

}

最后移除通知

-(void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:YES];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}

2.tableViewCell包含textField   在键盘出现和收回方法里做如下处理

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

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

#pragma mark 键盘出现

-(void)keyboardWillShow:(NSNotification *)note

{

CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - keyBoardRect.size.height);

}

#pragma mark 键盘消失

-(void)keyboardWillHide:(NSNotification *)note

{

tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );

}

textField数据保存

cell.textFiled.delegate = self;

- (void)textFieldDidEndEditing:(UITextField *)textField {

NSString *text = textField.text;

NSString *textIndexPath = [NSString stringWithFormat:@"%ld",(long)textField.tag];

//写一个字典用来保存数据

[self.textDict setObject:text forKey:textIndexPath];

}

3.tableView里既有textField还有textView,在使用第二个方法时发现textView输入时并不会移动视图,后来发现tableViewController已经帮我们处理好了 

@property (nonatomic,strong)UITableView *tableView;

[self.view addSubview:self.tableView];

- (UITableView *)tableView {

if (!_tableView) {

UITableViewController* tvc=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

[self addChildViewController:tvc];

[tvc.view setFrame:self.view.frame];

_tableView=tvc.tableView;

_tableView.delegate = self;

_tableView.dataSource = self;

_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

}

return _tableView;

}将tableView替换成tableViewController即可

本文仅做记录和个人参考,不对之处还望大家指出,共勉

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30070947/article/details/53996993
个人分类: iOS
上一篇iOS Xcode7.2开发时使用SBJson解析时 出现野指针的问题解决
下一篇iOS 自定义键盘收回按钮
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭