输入框随键盘移动的实现

主要用到的知识点:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值