IOS NSNotification 键盘遮挡问题的解决办法

发布时间 - 2026-01-11 03:29:47    点击率:

IOS NSNotification 键盘遮挡问题的解决办法

从键盘通知中获得键盘尺寸

键盘尺寸存在于NSNotification中。

1;在AddDrinkViewController中添加keyboardDidShow和keyboardDidHide方法

2;在viewWillAppear中注册UIKeyboardDidshowNotification与UIKeyboardDidHideNotification。

3;在viewWillDisappear中取消对所有事件的订阅注册

4;在AddDrinkViewController中添加一个Bool成员,跟踪键盘是否可见的状态。

//
// ViewController.h
// scrol
//
// Created by gao wuhang on 12-12-5.
// Copyright (c) 2012年 gao wuhang. All rights reserved.
//

#import

@interface ViewController : UIViewController{
  BOOL keyboardVisible;
  UIScrollView *scrollView;
}

- (void)keyboardDidShow: (NSNotification*) notif;
- (void)keyboardDidHide: (NSNotification*) notif;

@property (nonatomic, retain) UIScrollView *scrollView;
@end

 
//
// ViewController.m
// scrol
//
// Created by gao wuhang on 12-12-5.
// Copyright (c) 2012年 gao wuhang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize scrollView;

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void) keyboardDidShow:(NSNotification *)notif {
NSLog(@"%@", @"Received UIKeyboardDidShowNotification");
 
if (keyboardVisible) {
NSLog(@"%@", @"Keyboard is already visible. Ignoring notifications.");
return;
}
 
// The keyboard wasn't visible before
NSLog(@"Resizing smaller for keyboard");
 
// Get the origin of the keyboard when it finishes animating
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
 
// Get the top of the keyboard in view's coordinate system.
// We need to set the bottom of the scrollview to line up with it
CGRect keyboardRect = [aValue CGRectValue];
  keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
  
// Resize the scroll view to make room for the keyboard
  CGRect viewFrame = self.view.bounds;
viewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
 
self.scrollView.frame = viewFrame;
keyboardVisible = YES;
}

- (void) keyboardDidHide:(NSNotification *)notif {
NSLog(@"%@", @"Received UIKeyboardDidHideNotification");
 
if (!keyboardVisible) {
NSLog(@"%@", @"Keyboard already hidden. Ignoring notification.");
return;
}
 
// The keyboard was visible
NSLog(@"%@", @"Resizing bigger with no keyboard");
  
// Resize the scroll view back to the full size of our view
self.scrollView.frame = self.view.bounds;
keyboardVisible = NO;
}

- (void)viewDidLoad
{
  scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
//  scroll.contentSize = CGSizeMake(1000, 1000);
  [self.view addSubview:scrollView];
//  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//  [button setBackgroundColor:[UIColor blackColor]];
//  [scroll addSubview:button];
  UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
  textView.text = @"222";
  textView.font = [UIFont systemFontOfSize:20];
  [scrollView addSubview:textView];
  [super viewDidLoad];
  [textView release];

  self.scrollView.contentSize = self.view.frame.size;
// Do any additional setup after loading the view, typically from a nib.
}

- (void)dealloc
{
  [scrollView release];
  [super dealloc];
}

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end


如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


# IOS  # NSNotification  # 键盘遮挡  # 键盘遮挡如何解决  # iOS项目开发键盘弹出遮挡输入框问题解决方案  # IOS TextFiled与TextView 键盘的收起以及处理键盘遮挡  # 解决移动端 ios 系统键盘遮挡的问题  # iOS 防键盘遮挡的实例  # iOS中表单列表样式键盘遮挡的解决方案  # 如有  # 希望能  # 谢谢大家  # 解决办法  # 疑问请  # defaultCenter  # NSNotificationCenter  # animated  # super  # UIKeyboardDidShowNotification  # object  # addObserver  # selector  # notif  # property  # scrollView  # void  # implementation  # synthesize  # nonatomic 


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


相关推荐: Laravel如何使用Passport实现OAuth2?(完整配置步骤)  Laravel如何与Pusher实现实时通信?(WebSocket示例)  Windows家庭版如何开启组策略(gpedit.msc)?(安装方法)  JS中页面与页面之间超链接跳转中文乱码问题的解决办法  Laravel怎么定时执行任务_Laravel任务调度器Schedule配置与Cron设置【教程】  网站优化排名时,需要考虑哪些问题呢?  Laravel如何实现API版本控制_Laravel版本化API设计方案  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  如何基于云服务器快速搭建网站及云盘系统?  Laravel distinct去重查询_Laravel Eloquent去重方法  python中快速进行多个字符替换的方法小结  Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  Laravel如何创建自定义Artisan命令?(代码示例)  如何在阿里云域名上完成建站全流程?  标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南  uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址  Laravel如何使用查询构建器?(Query Builder高级用法)  如何彻底删除建站之星生成的Banner?  美食网站链接制作教程视频,哪个教做美食的网站比较专业点?  如何用5美元大硬盘VPS安全高效搭建个人网站?  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  制作旅游网站html,怎样注册旅游网站?  Laravel Admin后台管理框架推荐_Laravel快速开发后台工具  Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】  利用vue写todolist单页应用  Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧  专业商城网站制作公司有哪些,pi商城官网是哪个?  魔毅自助建站系统:模板定制与SEO优化一键生成指南  详解Android中Activity的四大启动模式实验简述  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  微信公众帐号开发教程之图文消息全攻略  C语言设计一个闪闪的圣诞树  如何用JavaScript实现文本编辑器_光标和选区怎么处理  网站制作软件免费下载安装,有哪些免费下载的软件网站?  韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐  晋江文学城电脑版官网 晋江文学城网页版直接进入  QQ浏览器网页版登录入口 个人中心在线进入  Python文本处理实践_日志清洗解析【指导】  公司网站制作需要多少钱,找人做公司网站需要多少钱?  头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  网站制作报价单模板图片,小松挖机官方网站报价?  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】  Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  EditPlus中的正则表达式 实战(1)  php485函数参数是什么意思_php485各参数详细说明【介绍】  北京网站制作的公司有哪些,北京白云观官方网站?  微信小程序 五星评分(包括半颗星评分)实例代码  bootstrap日历插件datetimepicker使用方法