美文网首页
ios-键盘上移

ios-键盘上移

作者: 紫嫣沁 | 来源:发表于2017-07-19 14:53 被阅读0次

1,两个通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];

2,实现两个方法:

//键盘弹出

-(void)keyboardWillShow:(NSNotification *)n

{

NSDictionary* userInfo = [n userInfo];

//UIKeyboardFrameEndUserInfoKey 要用这个key,别用错啦!!

CGSize keyboardSize =[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

CGRect frame = self.editAlert.frame;

NSInteger offset = frame.origin.y+frame.size.height - (kScreenHeight - keyboardSize.height);//键盘高度216

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

[UIView setAnimationDuration:animationDuration];

if(offset >= 0)

{

self.editAlert.sd_y = kScreenHeight - keyboardSize.height - self.editAlert.sd_height;

}

[UIView commitAnimations];

}

//键盘隐藏

-(void)keyboardWillHide:(NSNotification *)n

{

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

CGRect rect = CGRectMake(58.0f, (kScreenHeight-180)/2, kScreenWidth-58*2, 180);

self.editAlert.frame = rect;

[UIView commitAnimations];

[self.ContentText resignFirstResponder];

_keyboardIsShown= NO;

}

3,记得在dealloc中移除通知:

-(void)dealloc

{

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:self.ContentText];

}

相关文章

  • ios-键盘上移

    1,两个通知 [[NSNotificationCenter defaultCenter]addObserver:s...

  • 键盘上移

    -(void)defaultCenter{[[NSNotificationCenter defaultCenter...

  • 键盘上移问题

    1、输入框监听UIControlEventEditingDidBegin事件,当用户开始输入时,将整个view上移...

  • Flutter showDialog键盘上移

    写弹窗时发现弹窗里的输入框没有使界面上移,挡住了部分界面,很难受 想要界面随键盘上移需要把弹窗Widget按如下结...

  • iOS视图跟随键盘上移

    自我mark一下。如下图所示:界面上有2个textField,当其成为第一响应者时弹出键盘且要求textField...

  • iOS UITextField输入框随键盘弹出界面上移

    //点击输入框界面跟随键盘上移 (void)textFieldDidBeginEditing:(UITextFie...

  • ubuntu常用快捷键

    ubuntu系统快捷键: 测试系统:ubuntu16.04 ubuntu中的super就是键盘上的windows键...

  • Mac中delete键的5种用法

    mac中delete键的5种用法 第一种:按 delete 键,实现 Windows 键盘上退格键的功能,也就是删...

  • 全民诗人

    只要键盘上有回车键,伟大的诗歌就不难诞生。

  • 基础篇20-函数

    pycharm快捷键:shift+ctrl+↑,行上移

网友评论

      本文标题:ios-键盘上移

      本文链接:https://www.haomeiwen.com/subject/apulkxtx.html