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\]; }

相关推荐
Sim14802 小时前
iPhone将内置本地大模型,手机端AI实现0 token成本时代来临?
人工智能·ios·智能手机·iphone
Digitally3 小时前
如何将 iPad 上的照片传输到 U 盘(4 种解决方案)
ios·ipad
破烂儿5 小时前
TMUX历史输出滚动查看全攻略(原生快捷键 + 鼠标配置优化)
服务器·学习·计算机外设
报错小能手6 小时前
ios开发方向——swift并发进阶核心 @MainActor 与 DispatchQueue.main 解析
开发语言·ios·swift
LcGero6 小时前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb
ii_best6 小时前
lua语言开发脚本基础、mql命令库开发、安卓/ios基础开发教程,按键精灵新手工具
android·ios·自动化·编辑器
用户223586218201 天前
WebKit WebPage API 的引入尝试与自研实现
ios
啦啦啦!1 天前
ChatGPT和Gemini的接入和封装
人工智能·ios·chatgpt
aosky1 天前
手机远程畅玩OpenAI Codex
macos·智能手机·cocoa
开开心心就好1 天前
支持自定义名单的实用随机抽签工具
windows·计算机视觉·计算机外设·excel·散列表·启发式算法·csdn开发云