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

相关推荐
吴Wu涛涛涛涛涛Tao1 小时前
Flutter 实现「可拖拽评论面板 + 回复输入框 + @高亮」的完整方案
android·flutter·ios
搜狐技术产品小编20237 小时前
CAEmitterLayer:iOS 中创建炫酷粒子效果的魔法工具
macos·ios·objective-c·cocoa
00后程序员张1 天前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
sukalot1 天前
window显示驱动开发—头戴式和专用显示器
计算机外设
咕噜签名分发冰淇淋1 天前
内测分发是什么?
ios
2501_916007471 天前
Transporter App 使用全流程详解:iOS 应用 ipa 上传工具、 uni-app 应用发布指南
android·ios·小程序·https·uni-app·iphone·webview
白玉cfc1 天前
【OC】单例模式
开发语言·ios·单例模式·objective-c
Digitally1 天前
比较 iPhone:全面比较 iPhone 17 系列
android·ios·iphone
2501_915909062 天前
HTTPS 错误解析,常见 HTTPS 抓包失败、443 端口错误与 iOS 抓包调试全攻略
android·网络协议·ios·小程序·https·uni-app·iphone