博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 键盘的一些问题
阅读量:4676 次
发布时间:2019-06-09

本文共 3109 字,大约阅读时间需要 10 分钟。

首先就是获取键盘的尺寸,需要手动调用 registerForKeyboardNotifications 方法,其他两个会自动调用,弹出的键盘高 216(输入英文时候),

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    [self registerForKeyboardNotifications];

    UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];

    [self.view addSubview:tv];

    [tv release];

}

- (void) registerForKeyboardNotifications

{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];

}

 

- (void) keyboardWasShown:(NSNotification *) notif

{

    NSDictionary *info = [notif userInfo];

    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    

    NSLog(@"keyBoard:%f", keyboardSize.height);  //216

    ///keyboardWasShown = YES;

}

- (void) keyboardWasHidden:(NSNotification *) notif

{

    NSDictionary *info = [notif userInfo];

    

    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.height);

    // keyboardWasShown = NO;

    

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

 系统的一些键盘 监听通知

- (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    

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

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];

    

    

}

 

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

- (void)keyboardWillShow:(NSNotification *)aNotification

{

    //获取键盘的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    int height = keyboardRect.size.height;

}

 

//当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

    

}

有时候我们想在键盘弹起以后,在上面加上一个完成按钮,
主要是通过添加一个toolbar,上面的按钮可以使用系统的,也可以自定义。
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];    [topView setBarStyle:UIBarStyleBlackTranslucent];        UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.frame = CGRectMake(2, 5, 50, 25);    [btn addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];    [btn setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneBtn,nil];    [topView setItems:buttonsArray];    [textfield setInputAccessoryView:topView];-(void)dismissKeyBoard{    [textfield resignFirstResponder]; }
 

转载于:https://www.cnblogs.com/blogfantasy/p/5001236.html

你可能感兴趣的文章
Java替代C语言的可能性
查看>>
android ListView中CheckBox错位的解决
查看>>
linux下的mongodb数据库原生操作
查看>>
BNUOJ 1268 PIGS
查看>>
菜鸟的MySQL学习笔记(三)
查看>>
商业选址5A法则
查看>>
POJ 1191 棋盘分割(区间DP)题解
查看>>
文件同步服务器,iis 集群 ,代码同步(一)
查看>>
JS之模板技术(aui / artTemplate)
查看>>
【Tomcat】Tomcat Connector的三种运行模式【bio、nio、apr】
查看>>
Mysql-2-数据库基础
查看>>
python把源代码打包成.exe文件
查看>>
再也不用担心网吧开黑队友听不清了!降噪解决方案了解一下?
查看>>
PhotoshopCS5中将单位修改成百分比
查看>>
赵雅智:js知识点汇总
查看>>
变形二叉树中节点的最大距离(树的最长路径)——非递归解法
查看>>
cocos2d-x 3.0rc1 编译cpp-testsproject
查看>>
《Java虚拟机原理图解》1.3、class文件里的訪问标志、类索引、父类索引、接口索引集合...
查看>>
三种常见的图像处理双三次插值算法
查看>>
开玩笑html5(五岁以下儿童)---绕地球月球,地球绕太阳运动(canvas实现,同样可以移动哦)...
查看>>