iOS中只让textField使用键盘通知的实例代码

发布时间 - 2026-01-11 02:24:52    点击率:

代码:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  //为textField增加键盘事件
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addKeyboardNoti) name:UITextFieldTextDidBeginEditingNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeKeyboardNoti) name:UITextFieldTextDidEndEditingNotification object:nil];
}
#pragma -mark -keyboard notificatin
//键盘事件
- (void)keyboardWillShow:(NSNotification *)notification {
  NSDictionary *info = [notification userInfo];
  // keyboardHeight 为键盘高度
  CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  [self animateViewWithKeyboardHeight:keyboardSize.height];
}
- (void)keyboardWillHide:(NSNotification *)notification {
  [self animateViewWithKeyboardHeight:0.0];
}
- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight {
  NSTimeInterval animationDuration = 0.3f;
  CGFloat height = self.view.bounds.size.height;
  CGFloat width = self.view.bounds.size.width;
  CGFloat topSize = 0.0;
  CGFloat viewH = self.view.frame.size.height-64;
  CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height;
  CGFloat animateH = deviceHeight - viewH - keyboardHeight;
  if (animateH >= 0) {
    topSize = 0;
    CGRect toRect = CGRectMake(0, topSize, width, height);
    self.view.frame = toRect;
  } else {
    topSize = animateH;
    CGRect toRect = CGRectMake(0, topSize, width, height);
    [UIView animateWithDuration:animationDuration animations:^{
      self.view.frame = toRect;
    }];
  }
}
#pragma -mark -UITextFieldText Notification
//增加键盘事件
-(void)addKeyboardNoti
{
  NSLog(@"------addKeyboardNoti-------");
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//移除键盘事件
-(void)removeKeyboardNoti
{
  NSLog(@"------removeKeyboardNoti---------");
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
@end

总结

以上所述是小编给大家介绍的iOS中只让textField使用键盘通知的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# ios  # textField键盘通知  # iOS应用开发中监听键盘事件的代码实例小结  # iOS中的UIKeyboard键盘视图使用方法小结  # iOS实现输入框跟随键盘自动上移的实例代码  # iOS中键盘 KeyBoard 上添加工具栏的方法  # iOS项目开发键盘弹出遮挡输入框问题解决方案  # h5 ios输入框和键盘的兼容性优化指南  # iOS开发第三方键盘处理实例代码  # iOS自定义键盘切换效果  # IOS数字键盘左下角添加完成按钮的实现方法  # iOS自定义身份证键盘  # 小编  # 在此  # 给大家  # 所述  # 给我留言  # 感谢大家  # 移除  # 疑问请  # 有任何  # mark  # pragma  # notificatin  # keyboard  # UITextFieldTextDidEndEditingNotification  # addKeyboardNoti  # UITextFieldTextDidBeginEditingNotification  # object  # removeKeyboardNoti  # nil  # CGSize 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: nginx修改上传文件大小限制的方法  如何快速搭建支持数据库操作的智能建站平台?  Laravel如何生成URL和重定向?(路由助手函数)  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  html如何与html链接_实现多个HTML页面互相链接【互相】  Laravel如何实现模型的全局作用域?(Global Scope示例)  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  C++用Dijkstra(迪杰斯特拉)算法求最短路径  Laravel如何配置任务调度?(Cron Job示例)  Laravel如何升级到最新的版本_Laravel版本升级流程与兼容性处理  如何解决hover在ie6中的兼容性问题  如何用搬瓦工VPS快速搭建个人网站?  Laravel Pest测试框架怎么用_从PHPUnit转向Pest的Laravel测试教程  Laravel如何实现API速率限制?(Rate Limiting教程)  活动邀请函制作网站有哪些,活动邀请函文案?  C#如何调用原生C++ COM对象详解  php做exe能调用系统命令吗_执行cmd指令实现方式【详解】  网站制作软件有哪些,制图软件有哪些?  Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  如何选择可靠的免备案建站服务器?  iOS中将个别页面强制横屏其他页面竖屏  PHP正则匹配日期和时间(时间戳转换)的实例代码  制作旅游网站html,怎样注册旅游网站?  Laravel的HTTP客户端怎么用_Laravel HTTP Client发起API请求教程  Laravel如何使用.env文件管理环境变量?(最佳实践)  如何在服务器上三步完成建站并提升流量?  ChatGPT常用指令模板大全 新手快速上手的万能Prompt合集  如何快速启动建站代理加盟业务?  东莞市网站制作公司有哪些,东莞找工作用什么网站好?  什么是JavaScript解构赋值_解构赋值有哪些实用技巧  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  如何生成腾讯云建站专用兑换码?  Windows10电脑怎么设置虚拟光驱_Win10右键装载ISO镜像文件  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  香港服务器网站推广:SEO优化与外贸独立站搭建策略  怎样使用JSON进行数据交换_它有什么限制  javascript和jQuery中的AJAX技术详解【包含AJAX各种跨域技术】  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  如何在香港免费服务器上快速搭建网站?  Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】  Android Socket接口实现即时通讯实例代码  Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】  Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧  Laravel如何实现密码重置功能_Laravel密码找回与重置流程  Laravel怎么配置.env环境变量_Laravel生产环境敏感数据保护与读取【方法】  如何快速重置建站主机并恢复默认配置?  无锡营销型网站制作公司,无锡网选车牌流程?  如何用AWS免费套餐快速搭建高效网站?  Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)  Windows Hello人脸识别突然无法使用