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

相关推荐
春日见1 小时前
车辆动力学:前后轮车轴
java·开发语言·驱动开发·docker·计算机外设
锐意无限1 小时前
Swift 扩展归纳--- UIView
开发语言·ios·swift
符哥20081 小时前
用Apollo + RxSwift + RxCocoa搭建一套网络请求框架
网络·ios·rxswift
PHOSKEY2 小时前
光子精密QM系列闪测仪在鼠标电路板部件质量控制中的核心应用
计算机外设
Aftery的博客2 小时前
Xcode运行报错:SDK does not contain ‘libarclite‘ at the path
macos·cocoa·xcode
墩墩冰4 小时前
计算机图形学 分析选择缓冲区中的数字
计算机外设
文件夹__iOS6 小时前
AsyncStream 进阶实战:SwiftUI 全局消息流极简实现
ios·swiftui·swift
2501_916008898 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
UI设计兰亭妙微8 小时前
中车株州所显示器界面设计
计算机外设·界面设计
忆江南8 小时前
Flutter深度全解析
ios