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;

}

相关推荐
QWsin10 小时前
「轻漾」App隐私政策
ios·个人开发
season_zhu10 小时前
近段时间,AI加持下,我的开发感悟
ios
用户380341658829711 小时前
Flutter 迁 SwiftUI:AI 把页面"搬"过来只用了两周,我们补结构花了两个月
ios·swift
硅基手札13 小时前
【I2C 技术系列 05】I2C 多主仲裁与时钟同步
驱动开发·stm32·单片机·嵌入式硬件·mcu·计算机外设
硅基手札13 小时前
【I2C 技术系列 01】I2C 电气特性与物理层
驱动开发·stm32·单片机·嵌入式硬件·mcu·计算机外设
茶底世界之下14 小时前
四角变换后的最小包围视口,为什么不能只把宽高取整?
ios·swift
茶底世界之下14 小时前
羽毛颜色已经很鲜艳,还要继续加色吗?用 Awaking 试一次减饱和
前端·ios·运营
开开心心_Every16 小时前
内存清理工具定时自动清理开机自启动
java·开发语言·spring·计算机外设·ocr·intellij-idea·excel
天神哥哥啊17 小时前
cocos联调注意事项-IOS
ios·cocoa·xcode
终端安全笔记17 小时前
配置锁状态那一栏查的是什么:报告里几项各自指向什么风险
android·前端·安全·ios