主要用到的知识点:
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);
}