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
相关文章:
网站制作费用多少钱,一个网站的运营,需要哪些费用?
如何有效防御Web建站篡改攻击?
php条件判断怎么写_ifelse和switchcase的使用区别【对比】
建站之星如何实现网站加密操作?
宝塔面板创建网站无法访问?如何快速排查修复?
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
网站图片在线制作软件,怎么在图片上做链接?
Swift中switch语句区间和元组模式匹配
制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
如何通过山东自助建站平台快速注册域名?
建站主机SSH密钥生成步骤及常见问题解答?
建站之星CMS五站合一模板配置与SEO优化指南
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
常州自助建站:操作简便模板丰富,企业个人快速搭建网站
建站之星在线版空间:自助建站+智能模板一键生成方案
如何通过远程VPS快速搭建个人网站?
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
如何在自有机房高效搭建专业网站?
建站上市公司网站建设方案与SEO优化服务定制指南
北京制作网站的公司,北京铁路集团官方网站?
教程网站设计制作软件,怎么创建自己的一个网站?
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
高性价比服务器租赁——企业级配置与24小时运维服务
c++怎么用jemalloc c++替换默认内存分配器【性能】
如何通过西部数码建站助手快速创建专业网站?
子杰智能建站系统|零代码开发与AI生成SEO优化指南
如何在Golang中使用replace替换模块_指定本地或远程路径
如何快速搭建个人网站并优化SEO?
,怎么在广州志愿者网站注册?
C++如何将C风格字符串(char*)转换为std::string?(代码示例)
如何规划企业建站流程的关键步骤?
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
定制建站哪家更专业可靠?推荐榜单揭晓
一键制作网站软件下载安装,一键自动采集网页文档制作步骤?
如何在企业微信快速生成手机电脑官网?
香港服务器网站推广:SEO优化与外贸独立站搭建策略
如何通过多用户协作模板快速搭建高效企业网站?
网站制作壁纸教程视频,电脑壁纸网站?
微网站制作教程,我微信里的网站怎么才能复制到浏览器里?
成都网站制作公司哪家好,四川省职工服务网是做什么用?
黑客如何利用漏洞与弱口令入侵网站服务器?
北京网站制作公司哪家好一点,北京租房网站有哪些?
如何通过VPS建站无需域名直接访问?
一键网站制作软件,义乌购一件代发流程?
广州建站公司哪家好?十大优质服务商推荐
*请认真填写需求信息,我们会在24小时内与您取得联系。