代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
ObjC
】
避免弹出键盘遮盖住视图上的控件
作者:
万鑫
/ 发布于
2014/11/15
/
515
当需要输入文字时,弹出的键盘通常会将需要输入文字的UITextField遮盖住。这段代码利用scrollview来解决这个问题。
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window]; keyboardIsShown = NO; //make contentSize bigger than your scrollSize //(you will need to figure out for your own use case) CGSize scrollContentSize = CGSizeMake(320, 345); self.scrollView.contentSize = scrollContentSize; } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } - (void)dealloc { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWillHide:(NSNotification *)n { NSDictionary* userInfo = [n userInfo]; // get the size of the keyboard CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; // resize the scrollview CGRect viewFrame = self.scrollView.frame; // I'm also subtracting a constant kTabBarHeight because my UIScrollView // was offset by the UITabBar so really only the portion of the keyboard that // is leftover pass the UITabBar is obscuring my UIScrollView. viewFrame.size.height += (keyboardSize.height - kTabBarHeight); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; // The kKeyboardAnimationDuration I am using is 0.3 [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.scrollView setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = NO; } - (void)keyboardWillShow:(NSNotification *)n { // This is an ivar I'm using to ensure that we do not do the frame size // adjustment on the UIScrollView if the keyboard is already shown. // This can happen if the user, after fixing editing a UITextField, // scrolls the resized UIScrollView to another UITextField and attempts // to edit the next UITextField. If we were to resize the UIScrollView again, // it would be disastrous. NOTE: The keyboard notification will fire // even when the keyboard is already shown. if (keyboardIsShown) { return; } NSDictionary* userInfo = [n userInfo]; // get the size of the keyboard CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; // resize the noteView CGRect viewFrame = self.scrollView.frame; // I'm also subtracting a constant kTabBarHeight because my UIScrollView // was offset by the UITabBar so really only the portion of the keyboard // that is leftover pass the UITabBar is obscuring my UIScrollView. viewFrame.size.height -= (keyboardSize.height - kTabBarHeight); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; // The kKeyboardAnimationDuration I am using is 0.3 [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.scrollView setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = YES; }
试试其它关键字
视图上
scrollview
同语言下
.
根据生日计算星座
.
精简的实现一个内存池
.
iOS索引搜索核心代码
.
iOS 搜索框
.
文件操作
.
UIWebView 加载本地网页
.
取消导航条对视图的影响
.
利用终端显示 隐藏文件
.
更改导航条背景颜色
.
iOS 字体斜体
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
万鑫
贡献的其它代码
(
28
)
.
如何判断设备处于静音模式
.
播放简短的音乐
.
iOS实现在后台播放音乐
.
判断用户是否禁止该App使用设备麦克风
.
播放短暂的提示声音
.
自动关闭的Alert窗口
.
UIActionSheet动态添加按钮
.
生成ProgressView
.
在UIScrollView的delegate方法判断滚动快慢
.
判断scrollView是否滑动到底部
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3