iOS 键盘相关

1.键盘出现消失时,参考的view变化的代码:

  • (void)viewDidLoad {

superviewDidLoad\]; // Do any additional setup after loading the view. __weaktypeof(self) weakSelf =self; _textField= \[UITextField new\]; _textField.backgroundColor = \[UIColor redColor\]; \[self.view addSubview:_textField\]; \[_textField mas_makeConstraints:\^(MASConstraintMaker \*make) { //left,right,centerx,y不能共存只能有其二 make.left.mas_equalTo(20); //make.right.mas_equalTo(-60); make.centerX.equalTo(weakSelf.view); make.height.mas_equalTo(40); make.bottom.mas_equalTo(0); }\]; //注册键盘通知 \[\[NSNotificationCenter defaultCenter\] addObserver:selfselector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil\]; \[\[NSNotificationCenter defaultCenter\] addObserver:selfselector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil\]; } * (void)keyboardWillChangeFrameNotification:(NSNotification \*)notification { //获取键盘基本信息(动画时长与键盘高度) NSDictionary \*userInfo = \[notification userInfo\]; CGRect rect = \[userInfo\[UIKeyboardFrameBeginUserInfoKey\] CGRectValue\]; CGFloat keyboardHeight= CGRectGetHeight(rect); CGFloat keyboardDuration = \[userInfo\[UIKeyboardAnimationDurationUserInfoKey\] doubleValue\]; //修改下边距约束 \[_textField mas_updateConstraints:\^(MASConstraintMaker \*make) { make.bottom.mas_equalTo(-keyboardHeight); }\]; //更新约束 \[UIView animateWithDuration:keyboardDuration animations:\^{ \[self.view layoutIfNeeded\]; }\]; } * (void)keyboardWillHideNotification:(NSNotification \*)notification { //获得键盘动画时长 NSDictionary \*userInfo= \[notification userInfo\]; CGFloat keyboardDuration = \[userInfo\[UIKeyboardAnimationDurationUserInfoKey\] doubleValue\]; //修改为以前的约束(距下边距0) \[_textField mas_updateConstraints:\^(MASConstraintMaker \*make) { make.bottom.mas_equalTo(0); }\]; //更新约束 \[UIView animateWithDuration:keyboardDuration animations:\^{ \[self.view layoutIfNeeded\]; }\]; } * (void)touchesBegan:(NSSet \*)touches withEvent:(UIEvent \*)event { \[supertouchesBegan:touches withEvent:event\]; \[self.view endEditing:YES\]; }

相关推荐
ACP广源盛1392462567316 小时前
GSV2231G@ACP#2231G产品规格详解及产品应用分享
嵌入式硬件·计算机外设·音视频
专业开发者17 小时前
调试 iOS 蓝牙应用的新方法
物联网·macos·ios·cocoa
ACP广源盛1392462567319 小时前
GSV6505F@ACP#6505F产品规格详解及产品应用分享
单片机·嵌入式硬件·计算机外设·音视频
tangbin58308521 小时前
iOS Swift 可选值(Optional)详解
前端·ios
我的golang之路果然有问题1 天前
win键盘设置改为类似mac 配置
windows·笔记·macos·计算机外设·键盘
tyatyatya1 天前
MATLAB图形交互教程:鼠标拾取/坐标轴交互/动态绘图实战详解
开发语言·matlab·计算机外设
卷心菜加农炮1 天前
基于Python的FastAPI后端开发框架如何使用PyInstaller 进行打包与部署
ios
sc.溯琛2 天前
第 2 章 数据表示 核心重点提炼
计算机外设
北极象2 天前
千问大模型接入示例
ios·iphone·qwen
ipad协议开发2 天前
企业微信 iPad 协议应用机器人开发
ios·企业微信·ipad