对于iPhone界面控件的操作应该算是开发中必备的能力。键盘出现的时候上移相关的控件算是常见的需求,但是从这么多人问这个问题就可以看出,还是有很多人对这些需求的实现方式有疑问。
对于这个问题,主要是通过增加对键盘出现和消失的相应的Notification,然后在键盘出现和消息的时候,通过设置相关控件的frame来实现。相关代码如下,来源自stackoverflow。
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:_textField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)notif
{
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately
if ([_textField isFirstResponder] && self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (![_textField isFirstResponder] && self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
分享到:
相关推荐
UITextField+Extension 是 UITextField扩展 使用runtime实现,完美支持第三方键盘,实现上移效果 通过添加UITextField类目,使用runtime实现,TextField被遮挡时视图上移效果 点击对应的父视图的空白地方回收键盘,...
实现UITextField的键盘隐藏以及为了防止键盘挡住TextField而上移下移
UItextField和UItextView键盘覆盖问题,可以随键盘上高的改变,自动移动父类UIview
ios7隐藏虚拟键盘,解决键盘挡住UITextField问题
UITextField自动每四格添加一个空格,支持回删
对UITextField的类的扩展。 很好用的键盘扩展及键盘回收的使用 只要调用一句话方法 键盘随心所以
UITextField或者UITextView放在UIScrollView上弹出键盘修改UIScrollView上移。 还是有点小小的不完美,并不影响整体使用
不愿意下载的可以看代码地址 http://blog.csdn.net/darkmengziyan2013/article/details/43528445
UITextView 实现UITextField功能
iOS文本框,有时候需要用到类似炒股软件添加百分比或者数字等
iOS UITextField 扩展 密码框
通过添加UITextField类目,使用runtime实现,TextField被遮挡时视图上移效果,点击空白回收键盘,不需要写一句代码,所有TextField全拥有此功能,可以控制是否开启,上移视图,以及距keyboard距离 更新:1.解决...
使用Swift编写的字母和数字键键盘,可以用于UITextField和UITextView
这样的话按钮的事件也就触发不了,最好的解决办法就是当输入这些信息的时候让整个屏幕上移一个键盘的位置,或者上移到指定的位置。 首先一般输入的话都用的是UITextField,所以要监听用户什么时候开始输入和什么时候...
UITextField 文本字段控件 小Demo
例如用户登陆界面,一般都是UITextField,当我们点击输入框时会弹出系统键盘,这时如果不调整系统输入框,输入框可能会被键盘覆盖,这种用户体验很不友好,所以这时我们应该让输入框也随着键盘的升起相应的调整坐标...
该文档详细列出UITextField常用以及不常用的相关属性及如何进行设置,对大家掌握UITextField的用法很有帮助。
本demo主要是解决一般限制输入字符数量方法存在的一个bug,就是在将要超过限制时输入一串非常长的字符串,选择字符串时会提示超过限制问题;
使用通知,动态获取UITextField中汉字个数
UITextFieldBlockDemo