美文网首页
iOS 点击按钮唤起键盘

iOS 点击按钮唤起键盘

作者: 路边的风景呢 | 来源:发表于2020-11-11 11:27 被阅读0次

KeyboardView.h 文件

@protocol KeyboardViewDelegate <NSObject>

@required

-(void)uploadMessage:(NSString* )text;

@end

@interface KeyboardView : UIView

// 代理

@property(nonatomic,weak)id <KeyboardViewDelegate> delegate;

@property(nonatomic,strong)UITextView * textView;

-(void)setTextViewPlaceholder:(NSString *)text;

@end

KeyboardView.m 文件

@interface KeyboardView ()<UITextViewDelegate>{

    BOOLstatusTextView;

    NSString* placeholderText;

}

@property(nonatomic,strong)UIButton * emptyBtn;

@property(nonatomic,strong)UIView * backGroundView;

@property(nonatomic,strong)UILabel * placeholderLabel;

@property(nonatomic,strong)UIButton * senderButton;

@end

@implementation KeyboardView

-(instancetype)initWithFrame:(CGRect)frame{

    if(self== [superinitWithFrame:frame]) {

        [selfcreateUI];

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

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

    }

    return self;

}

-(void)createUI{

    // 添加视图

    [self addSubview:self.backGroundView];

    self.textView.frame=CGRectMake(5,5,SCREEN_WIDTH-75,36);

    self.placeholderLabel.frame=CGRectMake(10,10,SCREEN_WIDTH-65,24);

    self.senderButton.frame=CGRectMake(SCREEN_WIDTH-60,5,50,36);

    [self.backGroundView addSubview:self.textView];

    [self.backGroundView addSubview:self.placeholderLabel];

    [self.backGroundView addSubview:self.senderButton];

    // 空白部分

    [selfaddSubview:self.emptyBtn];

}

-(void)textViewDidChange:(UITextView*)textView{

    if(textView.text.length==0) {

        self.placeholderLabel.text = placeholderText;

        self.senderButton.backgroundColor = [UIColor grayColor];

        self.senderButton.userInteractionEnabled = NO;

    }else{

        self.placeholderLabel.text = @"";

        self.senderButton.userInteractionEnabled = YES;

        self.senderButton.backgroundColor = [UIColor redColor];

    }

}

-(void)setTextViewPlaceholder:(NSString *)text{

    placeholderText = text;

    self.placeholderLabel.text = placeholderText;

}

-(void)keyBoardWillShow:(NSNotification*)notific{

    // 修改 父视图 的 frame

    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    NSDictionary* info = [notificuserInfo];

    NSValue * value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRectkeyboardInfo = [valueCGRectValue];

    CGFloatheight = keyboardInfo.size.height;

    // 修改 操作视图 的 frame

    self.backGroundView.frame = CGRectMake(0, SCREEN_HEIGHT - showHeight - height, SCREEN_WIDTH, showHeight);

    self.emptyBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - showHeight - height);

}

-(void)keyBoardhidenShow:(NSNotification *)notific{

    self.backGroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, showHeight);

    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100);

}

-(void)sendBtnClick{

    if(self.delegate&& [self.delegaterespondsToSelector:@selector(uploadMessage:)]) {

        [self.delegate uploadMessage:self.textView.text];

    }

    self.textView.text=@"";

    self.placeholderLabel.text = placeholderText;

    self.senderButton.backgroundColor = [UIColor grayColor];

    self.senderButton.userInteractionEnabled = NO;

    [self.textView resignFirstResponder];

}

-(void)emptyBtnClick{

    [self.textView resignFirstResponder];

}

-(UIView *)backGroundView{

    if(!_backGroundView){

        _backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 49)];

        _backGroundView.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];

    }

    return  _backGroundView;

}

-(UITextView *)textView{

    if(!_textView){

        _textView= [[UITextViewalloc]init];

        _textView.font = [UIFont systemFontOfSize:16];

        _textView.delegate=self;

    }

    return  _textView;

}

-(UILabel *)placeholderLabel{

    if(!_placeholderLabel){

        _placeholderLabel= [[UILabelalloc]init];

        _placeholderLabel.font = [UIFont systemFontOfSize:16];

        _placeholderLabel.textColor = [UIColor grayColor];

    }

    return  _placeholderLabel;

}

-(UIButton *)senderButton{

    if(!_senderButton){

        _senderButton= [[UIButtonalloc]init];

        [_senderButton setTitle:@"发送" forState:UIControlStateNormal];

        _senderButton.backgroundColor = [UIColor grayColor];

        _senderButton.userInteractionEnabled = NO;

        [_senderButton addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];

    }

    return  _senderButton;

}

-(UIButton *)emptyBtn{

    if(!_emptyBtn){

        _emptyBtn= [[UIButtonalloc]init];

        _emptyBtn.backgroundColor = [UIColor clearColor];

        [_emptyBtn addTarget:self action:@selector(emptyBtnClick) forControlEvents:UIControlEventTouchUpInside];

    }

    return  _emptyBtn;

}

@end

相关文章

  • iOS 点击按钮唤起键盘

    KeyboardView.h 文件 @protocol KeyboardViewDelegate

  • iOS 防止button重复点击

    iOS防止button重复点击 按钮点击控制处理-Runtime IOS应用防止按钮连续点击 - 简书

  • iOS 如何在键盘上方增加“收起“键盘按钮

    iOS 如何在键盘上方增加“收起“键盘按钮:

  • QAP手动关闭键盘

    QAP安卓端在弹出键盘时,我们点击键盘上的完成按钮,键盘会自动收起。如果输入完成后未点击完成按钮,直接点击保存等相...

  • Swift之UITextField 键盘(一)

    方式1 给键盘添加完成按钮 1、创建 2、设置 3、点击事件 这样点击键盘上的完成按钮 键盘就收缩回去了。

  • ReactiveCocoa学习之路

    ReactiveCocoa 前言 在 iOS 编程中我们需要处理各种事件,例如响应按钮的点击,监听键盘的输入,监听...

  • textfield

    不让键盘挡住,UITextField键盘点击Done按钮隐藏键盘 UITextFieldhttp://www.pr...

  • vue中button取消键盘监听

    一般情况下,点击按钮后,按钮就获取焦点,再点击键盘上的健,就会响应按钮的点击事件。(点击按钮时,取消按钮的焦点,再...

  • Xcode9 iOS11适配

    1.iOS11UIToolBar上添加的按钮点击实效 测试总结:iOS11一下按钮可以正常点击iOS11以上按钮点...

  • iOS 关于AlertView与键盘冲突解决

    背景:在开发过程中,iOS8之后,当触发textField时键盘响应,此时如果点击按钮弹出UIAlertView点...

网友评论

      本文标题:iOS 点击按钮唤起键盘

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