Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 545|回复: 0

iOS键盘遮挡问题解决办法

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-7-15 02:54:06 | 显示全部楼层 |阅读模式

    iOS开发之“键盘遮挡输入框的解决办法”之一 -----键盘通知之前处理这种问题,总是在触发输入框编辑事件键盘弹出的时候,将当前的View整体向上移动,结束编辑又整体向下移,耗时耗力效率低。

    在网上看了使用键盘通知的方法很是方便,所以写了个demo供初学者参考!

     

    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

    6.结束!

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-20 01:27 , Processed in 0.064096 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表