输入框随键盘移动的实现

来源:互联网 发布: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);  

}

实现的效果图:


0 0