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

相关推荐
ab_dg_dp1 小时前
解决Ubuntu系统鼠标不流畅的问题
linux·ubuntu·计算机外设
1024小神6 小时前
在swiftui中使用实现类似微信右上角的功能菜单
ios
北京自在科技8 小时前
iOS 18.4修复多个核心安全漏洞,间接增强Find My服务的数据保护能力
科技·ios·iphone·蓝牙·find my·北京自在科技
货拉拉技术11 小时前
iOS货运用户App组件路由器设计与实践
ios·架构
布多11 小时前
iOS分类中的关联对象:如何用运行时突破Category的存储限制
ios·面试·源码
洛克希德马丁13 小时前
Qt饼状图在图例上追踪鼠标落点
开发语言·qt·计算机外设
鸿蒙布道师1 天前
鸿蒙NEXT开发数值工具类(TS)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
程序员老刘1 天前
谨慎升级macOS 15.4,规避 ITMS-90048 错误
flutter·macos·ios
90后的晨仔1 天前
iOS 蓝牙开发基础知识梳理
ios
一牛1 天前
Metal 进阶:读取可绘制对象的像素
ios·swift·计算机图形学