UI页面输入框被键盘遮挡问题解决方案

//在控制器页面输入框键盘回收、键盘遮挡输入框问题处理方案
- (void)viewDidLoad {
    [super viewDidLoad];    
self.textFieldJob = UITextField.alloc.init;
    self.textFieldJob.frame = CGRectMake(140, self.view.frame.size.height-50, 100, 30);
    self.textFieldJob.placeholder = @"请输入";
    self.textFieldJob.layer.borderColor = [UIColor redColor].CGColor;
    self.textFieldJob.layer.borderWidth = 1.0;
    self.textFieldJob.borderStyle = UITextBorderStyleRoundedRect;
    self.textFieldJob.textColor = UIColor.redColor;
    self.textFieldJob.delegate = self;
    [self.view addSubview:self.textFieldJob];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)note {
    CGRect keyBoardRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.view.frame = CGRectMake(0, -keyBoardRect.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
}
- (void)keyboardWillHide:(NSNotification*)note{

    self.view.frame = self.view.bounds;

}
//在自定义cell中键盘回收、输入框键盘遮挡问题处理方案
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.textFieldJob = UITextField.alloc.init;
        self.textFieldJob.frame = CGRectMake(140, 0, 100, 30);
        self.textFieldJob.placeholder = @"请输入";
        self.textFieldJob.borderStyle = UITextBorderStyleRoundedRect;
        self.textFieldJob.textColor = UIColor.redColor;
        self.textFieldJob.delegate = self;
        [self.contentView addSubview:self.textFieldJob];
        
    }
    return self;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {//开始编辑
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)note {
    CGRect keyBoardRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    UITableView *tmpView = (UITableView *)self.contentView.superview.superview;
    tmpView.contentInset = UIEdgeInsetsMake(0,0,keyBoardRect.size.height,0);
}

- (void)keyboardWillHide:(NSNotification*)note{
    UITableView *tmpView = (UITableView *)self.contentView.superview.superview;
    tmpView.contentInset = UIEdgeInsetsZero;
}

//备注:_tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;//滑动回收键盘

你可能感兴趣的:(ios,objective-c,ipad)