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;

}

相关推荐
思盛iOS签名上架5 小时前
App苹果签名有什么作用?
macos·ios·cocoa
CocoaKier7 小时前
Facebook广告后台收不到Adjust回传数据,我们排查了一个月终于找到了根因
ios·facebook
每周报刊8 小时前
竞速座舱级视界:爱攻 AGP497UCZD 超宽 QDOLED 显示器解析
数据结构·游戏·计算机外设
无定义_13 小时前
萌新联赛(六)
macos·objective-c·cocoa
FairGuard手游加固1 天前
iOS 游戏加固技术解析:代码混淆、资源加密、内存保护与重签名检测
安全·游戏·macos·unity·ios·objective-c
思盛iOS签名上架1 天前
iOS企业签名原理是什么?
macos·ios·cocoa
Daniel_Coder1 天前
我用 SwiftUI + GRDB 做了一个「按时吃药·服药提醒」App
ios·swiftui·swift·widgetkit
小幂同学1 天前
笔记本触控板能用鼠标连点器吗?快捷键与位置问题解析
计算机外设·鼠标连点器·连点器
Patrick_Wilson2 天前
iOS 第三方浏览器图片下载失败问题
前端·ios·浏览器