博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发中如何在键盘弹出时改变View的高度
阅读量:7294 次
发布时间:2019-06-30

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

在iOS开发的时候有两个经常要用到的控件UITextfield跟UITextView,我们输入内容基本是通过这两个控件进行的,但是有时候会遇到这样的问题:在点击输入之后弹出键盘遮盖住了输入框,可以通过以下办法解决:

添加通知监听键盘的弹出跟隐藏

//监听键盘弹出和隐藏    [[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 *)note{    //取得键盘弹出时间    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];    //取得键盘高度    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];    CGFloat keyboardHeight = keyboardFrame.size.height;    //option的值设置为7 << 16会让view跟键盘弹出效果同步    [UIView animateWithDuration:duration delay:0 options:7 << 16 animations:^{        self.ContentSv.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);    } completion:nil];}- (void)keyboardWillHide:(NSNotification *)note{    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];    [UIView animateWithDuration:duration delay:0 options:7 << 16 animations:^{        self.ContentSv.transform = CGAffineTransformIdentity;    } completion:nil];}

 

转载于:https://www.cnblogs.com/shaohuaios/p/4059588.html

你可能感兴趣的文章
(译)理解 LSTM 网络 (Understanding LSTM Networks by colah)
查看>>
java 反射
查看>>
[Android1.6]继承BaseAdapter为GridView设置数据时设置setLayoutParams时注意
查看>>
Winform开发几个常用的开发经验及知识积累(一)
查看>>
【FTP】java FTPClient 文件上传内容为空,文件大小为0
查看>>
Winform开发框架里面使用事务操作的原理及介绍
查看>>
Error:(108) No resource identifier found for attribute &#39;style&#39; in package &#39;android&#39;
查看>>
从JavaScript函数重名看其初始化方式
查看>>
用系统滚动条实现NumericUpDown的原理
查看>>
就不可以认真点么.
查看>>
JSP标签JSTL(4)--URL
查看>>
DiscuX END - 553 Envolope sender mismatch with header from..
查看>>
论文笔记之:Instance-aware Semantic Segmentation via Multi-task Network Cascades
查看>>
单例模式
查看>>
你能用微信小程序打开小程序了【附开发方法】
查看>>
【BOOM】一款有趣的Javascript动画效果
查看>>
Osmocom-bb系统编译
查看>>
SQL Server-聚焦深入理解动态SQL查询(三十二)
查看>>
高考查分数微信就能搞定
查看>>
ORM简介
查看>>