zoukankan      html  css  js  c++  java
  • 键盘遮挡输入框的问题

    1.在ViewController.m文件声明

    复制代码
    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
    @property(nonatomic,strong)UITableView *tableView;//自定义表格TableView
    @end
    
    @implementation ViewController
    复制代码


    2.初始化及添加通知观察者

    复制代码
     1 - (void)viewDidLoad {
     2     [super viewDidLoad]; 4     self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
     5     self.tableView.delegate = self;
     6     self.tableView.dataSource  = self;
     7     [self.view addSubview:self.tableView];
     8     
     9     //键盘将要显示时候的通知
    10     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    11      //键盘将要结束时候的通知
    12     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    13 }
    复制代码

    3.实现通知的响应方法

    复制代码
     1 -(void)boardWillShow:(NSNotification *)sender{
     2     //获得键盘的尺寸
     3     CGRect keyBoardRect=[sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];                           
     4     //当键盘将要显示时,将tableView的下边距增跟改为键盘的高度
     5     self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
     6 }
     7 
     8 -(void)boardDidHide:(NSNotification *)sender{
     9     //当键盘将要消失时,边距还原初始状态
    10     self.tableView.contentInset = UIEdgeInsetsZero;
    11 }
    复制代码

    4.UITextField的代理事件(点击键盘中的return按钮,隐藏键盘)

    1 -(BOOL)textFieldShouldReturn:(UITextField *)textField{
    2     //取消当前输入框的第一响应者
    3      [textField resignFirstResponder];
    4     return YES;
    5 }

    5.tableView的代理方法

    复制代码
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 15;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *ider = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ider];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ider];
        }
        
        UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 150, 20)];
        TF.placeholder = @"请输入";
        TF.delegate =self; //文本框添加代理
        [cell.contentView addSubview:TF];
        cell.textLabel.text = @"测试";
    
        return cell;
    }
    @end
    复制代码

     

  • 相关阅读:
    黑马程序员:3分钟带你读懂C/C++学习路线
    大学毕业的你,满腔洪荒之力却找不到出口?
    从零基础到精通的前端学习路线
    Python学习笔记(2)——Python的函数、模块、包和库
    Python学习笔记(1)——Python的概述(Python的环境、变量、数据类型、基本运算)
    MATLAB优化——减少for的使用
    初识Python(windows)——下载、安装、使用
    高维数据稀疏表示-什么是字典学习(过完备词典)
    用1天快速上手org-mode(windows系统)
    零基础数据挖掘学习清单
  • 原文地址:https://www.cnblogs.com/jx66/p/5714016.html
Copyright © 2011-2022 走看看