美文网首页
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 点击按钮唤起键盘

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