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即可

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值