UItextField自动随键盘

//增加监听,当键盘出现或改变时收出消息

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification
object:nil];

//增加监听,当键退出时收出消息

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification
object:nil];

#pragma mark - 登陆按钮位置变动

//当键盘出现或改变时调用

- (void)keyboardWillShow:(NSNotification *)aNotification {

//获取键盘的高度

NSLog(@"%f",self.landButton.frame.origin.y);

NSDictionary *userInfo = [aNotification userInfo];

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

int height = keyboardRect.size.height;

if (height==0) {

height =
282;

}

if (keyboardSportBool==NO) {

keyboardSportBool=YES;

CGRect landButtonRect =
self.landButton.frame;

CGRect loginLabelRect =
self.loginLabel.frame;

landButtonRect.origin.y-=(height/2);

loginLabelRect.origin.y-=(height/2);

self.landButton.frame = landButtonRect;

self.loginLabel.frame = loginLabelRect;

}

NSLog(@"%f",self.landButton.frame.origin.y);

}

//当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification{

NSDictionary *userInfo = [aNotification userInfo];

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

int height = keyboardRect.size.height;

if (keyboardSportBool==YES) {

keyboardSportBool=NO;

CGRect landButtonRect =
self.landButton.frame;

CGRect loginLabelRect =
self.loginLabel.frame;

landButtonRect.origin.y+=(height/2);

loginLabelRect.origin.y+=(height/2);

self.landButton.frame = landButtonRect;

self.loginLabel.frame = loginLabelRect;

}

}

时间: 2024-08-28 02:10:06

UItextField自动随键盘的相关文章

[菜鸟成长记]iOS开发自学笔记07-UITextField自动隐藏键盘

点击UITextField输入框后,软键盘默认情况下在结束输入后不会自动隐藏,目前总结了两种可以自动隐藏软键盘的方法: 第一种:通过响应UITextField对象的Did End On Exit事件,当点击默认软键盘的"Done"按钮或"Return"按钮会触发Did End On Exit 事件,通过编码实现来响应Did End On Exit事件来实现自动隐藏键盘 - (IBAction)TextFieldDidEndOnExit:(id)sender { [t

iOS开发-自动隐藏键盘及状态栏

1.隐藏状态栏 iOS升级至7.0以后,很多API被废止,其中原有隐藏状态栏StatusBar的方法就失效了. 原有方案 [[UIApplication sharedApplication] setStatusBarHidden:YES]; 但很不幸,在后来的版本中实效了,因此我们可以使用新的API来实现隐藏状态的栏的目的,如下: -(BOOL)prefersStatusBarHidden { return YES; } 2.自动隐藏键盘并自动布局 这里提供的解决方案适用于任何类型的键盘,主要是

用视图上移解决UITextField/UITextView被键盘遮盖问题

先看看UILabel/UITextField/UITextView的区别: UILabel 显示的文本只读,无法编辑,可以根据文字个数自动换行:UITextField 可编辑本文,但是无法换行,只能在一行显示:当点击键盘上的return时会收到一个事件做一些事情.UITextView 可编辑文本,提供换行功能. 解决UITextField/UITextView被键盘遮盖问题可以主要参考https://gist.github.com/ruandao/9429305 先记着,有时间做一些整理 参考链

一行代码为UITextField添加收键盘功能

iOS开发中收键盘是十分常用的功能,只需一行代码即可为ViewController添加工具条收键盘功能 更重要的是使用catogory,无代码污染. 代码 // UITextField+keyboard.h // TextFieldKeyBord // // Created by luo.h on 15/10/8. // Copyright © 2015年 l.h. All rights reserved. // #import <UIKit/UIKit.h> @interface UITex

UITextField弹出键盘挡住输入框问题

//开始编辑输入框的时候,软键盘出现,执行此事件 -(void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame = [textField convertRect:textField.frame toView:self.view]; //上移数值,根据自己情况调整 int offset = frame.origin.y - 100 - (UISCROEEN_SIZE.height - 216.0);//键盘高度216

js 实现自动调出键盘

在app中,在页面加载完成之后,给输入框添加一个focus,不能自动调出软键盘,可以用以下方式实现: //触发键盘 $("#content").on("touchstart", function () { $(this).focus(); }) $("#content").trigger("touchstart");

使用UITextField自动格式化银行卡号

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *text = [textField text]; NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"01234

运动的小球自动变键盘控制

<!DOCTYPE html>,<html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .box{ width: 400px;height: 400px;border: 1px solid black;position: relative;} .ball{width: 30px;heig

TableView滚动、自动收起键盘

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;} 请勿转载!