输入框随键盘移动的实现
来源:互联网 发布:ubuntu新建文件夹 编辑:程序博客网 时间:2024/05/19 16:19
主要用到的知识点:
1.通过通知 检测键盘显示的状态
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark ------键盘的状态------
- (void)keyBoard:(NSNotification *)note
{
NSDictionary *info = note.userInfo;
NSLog(@"info%@",info);
}
运行结果:1.键盘弹出时的结果
info{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
}
2.键盘回收时结果
info{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 538}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 796}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
以下是具体实现的代码:
@interface ViewController ()<UITextViewDelegate>
{
UIView *bgView;
UITextView *inputTextView;
CGRect keyBoardRect;
NSMutableArray *allContent;
UIButton *sendButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoard:) name:UIKeyboardWillHideNotification object:nil];
// 需要输入框和上面的按钮同时上移
bgView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-40, CGRectGetWidth([UIScreen mainScreen].bounds), 40)];
bgView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:bgView];
inputTextView = [[UITextView alloc]initWithFrame:CGRectMake(50, 5, 200, 30)];
inputTextView.layer.cornerRadius = 30/5;
inputTextView.delegate = self;
[bgView addSubview:inputTextView];
sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
sendButton.frame = CGRectMake(300-20, 5, 80, 30);
sendButton.backgroundColor = [UIColor whiteColor];
sendButton.tag = 100;
sendButton.layer.cornerRadius = 30/5;
[sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 默认不可以使用按钮
sendButton.enabled = NO;
[sendButton setTitle:@"发送" forState:UIControlStateNormal];
[sendButton addTarget:self action:@selector(sendContent:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:sendButton];
allContent = [NSMutableArray array];
}
- (void)sendContent:(UIButton *)sender
{
[inputTextView resignFirstResponder];
NSLog(@"%@",inputTextView.text);
NSDictionary *info = @{@"content":inputTextView.text};
[allContent addObject:info];
// 根据描述的数组 进行排序
allContent = [[allContent sortedArrayUsingDescriptors:sortDescriptorArr]mutableCopy];
NSLog(@"%@",allContent);
inputTextView.text= @"";
sender.enabled = NO;
}
//开始编辑的时候 允许 发送按钮使用
- (void)textViewDidBeginEditing:(UITextView *)textView
{
UIButton *button = (UIButton *)[bgView viewWithTag:100];
button.enabled = YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// 通过UITextView输入的内容 的到他的内容高度 把内容的高度设置成inputView的高度 以及bgView的高度
bgView.frame = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds) - textView.contentSize.height-10-CGRectGetHeight(keyBoardRect), CGRectGetWidth([UIScreen mainScreen].bounds), textView.contentSize.height+10);
inputTextView.frame = CGRectMake(50, 5, 200, textView.contentSize.height);
return YES;
}
#pragma mark ------键盘的状态------
- (void)keyBoard:(NSNotification *)note
{
NSDictionary *info = note.userInfo;
NSLog(@"info%@",info);
keyBoardRect = [info[UIKeyboardFrameEndUserInfoKey]CGRectValue];
bgView.frame = CGRectMake(0, CGRectGetMinY(keyBoardRect)-40, CGRectGetWidth([UIScreen mainScreen].bounds), 40);
}
- 输入框随键盘移动的实现
- 关于输入框随键盘移动的方法
- swift 输入框随键盘移动
- iOS - 设置底部输入框随键盘的移动而移动
- iOS输入框随键盘滑动的另类实现
- ios输入框随键盘上下移动动画
- IOS4与IOS5输入框随键盘移动问题
- h5移动端输入框随键盘上升
- 移动web 键盘挡住输入框
- 监听键盘位置,使输入框跟随键盘移动
- 当键盘挡住输入框的时候让输入框往上移动
- ios UItextFiewld 点击文本框编辑 实现弹出键盘时,输入框向上移动
- iOS 实现输入框被编辑以及取消编辑跟随键盘移动
- js 移动端的输入金额的键盘
- [Phonegap+Sencha Touch] 移动开发68 Sencha Touch弹出键盘挡住输入框的解决办法
- 移动端输入框与底部定位键出现的键盘弹出问题
- 移动端固定输入框在底部会被键盘遮挡的解决方法
- 移动端虚拟键盘遮挡页面输入框等元素的解决方案
- Java小技巧
- [总结]视音频编解码技术零基础学习方法- 理论
- hdu 1158 Employment Planning
- 01 程序启动工具
- 使用GET提交form
- 输入框随键盘移动的实现
- linux 自己用到的一些命令,留下备忘。
- C++primer:string流
- Ubuntu64位(阿里云ECS)配置Java_Web环境(jdk,mysql,tomcat)
- iOS 多线程的使用情况
- 数据结构实验1_顺序表的操作
- 远程调用服务(RPC)和消息(Message Queue)对比及其适用/不适用场合
- CATransition-转场动画
- 回忆录之软件管理