键盘上方添加自定义view

2022-12-04  本文已影响0人  倒着游的鱼

controller类

@property(nonatomic,strong) PPAKeyboardToolBar * keyboardToolBar;

- (void)viewDidLoad {
    [super viewDidLoad];

//增加监听,当键盘出现或改变时收出消息
    [self.view addSubview:self.keyboardToolBar];
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    //增加监听,当键退出时收出消息
    
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(keyboardWillHide:)  name:UIKeyboardWillHideNotification object:nil];

}


#pragma mark 当键盘出现或改变时调用

- (void)keyboardWillShow:(NSNotification*)aNotification {
    
    //键盘弹出时显示工具栏
    //获取键盘的高度
    NSDictionary*userInfo = [aNotification userInfo];
    
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect = [aValue CGRectValue];
    
    float keyBoardHeight = keyboardRect.size.height;
    // NSLog(@"%ld",(long)keyBoardHeight);
    
    [UIView animateWithDuration:0.1 animations:^{
        
        self.keyboardToolBar.frame = CGRectMake(0, SCREEN_HEIGHT-keyBoardHeight-keightRealValue(50), SCREEN_WIDTH,  keightRealValue(50));
        
    }];
    
}

#pragma mark 当键退出时调用

- (void)keyboardWillHide:(NSNotification*)aNotification {
    
    //键盘消失时 隐藏工具栏
    [UIView animateWithDuration:0.1 animations:^{
        self.keyboardToolBar.frame = CGRectMake(0, SCREEN_HEIGHT+keightRealValue(50), SCREEN_WIDTH, keightRealValue(50));
        
    }];
    
}

- (PPAKeyboardToolBar *)keyboardToolBar {
    if (_keyboardToolBar == nil) {
        _keyboardToolBar = [[PPAKeyboardToolBar alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT+keightRealValue(50), SCREEN_WIDTH, keightRealValue(50))];
    }
    return _keyboardToolBar;
}

PPAKeyboardToolBar.m 类

#import "PPAKeyboardToolBar.h"

@interface PPAKeyboardToolBar ()
@property (nonatomic, strong)UIButton *voiceButton;//语音
@property (nonatomic, strong)UIButton *albumButton; //相册
@property (nonatomic, strong)UIButton *cameraButton;//相机拍照
@property (nonatomic, strong)UIButton *videoButton;//录像

@end

@implementation PPAKeyboardToolBar

- (instancetype)initWithFrame:(CGRect)frame{

    if(self= [super initWithFrame:frame]) {
        [self addSubview:self.voiceButton];
        [self addSubview:self.albumButton];
        [self addSubview:self.cameraButton];
        [self addSubview:self.videoButton];

        //UI 布局
        [self setUI];
    }

    return self;
}

#pragma mark-- 布局

- (void)setUI{

    [self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self).with.offset(kRealValue(10));
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
        make.width.mas_equalTo((SCREEN_WIDTH-kRealValue(20))/4);
    }];



    [self.albumButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.voiceButton.mas_right).with.offset(0);
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
        make.width.mas_equalTo((SCREEN_WIDTH-kRealValue(20))/4);

    }];



    [self.cameraButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.albumButton.mas_right).with.offset(0);
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
        make.width.mas_equalTo((SCREEN_WIDTH-kRealValue(20))/4);
    }];

    [self.videoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cameraButton.mas_right).with.offset(0);
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
        make.width.mas_equalTo((SCREEN_WIDTH-kRealValue(20))/4);
    }];

}

- (UIButton*)voiceButton{
    if(_voiceButton==nil) {
        _voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_voiceButton setTitle:@"语音 " forState:UIControlStateNormal];
        _voiceButton.titleLabel.font=[UIFont systemFontOfSize:14];
        //设置图片和按钮的间距
        [_voiceButton layoutButtonWithEdgeInsetsStyle:GMButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        [_voiceButton setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
        [_voiceButton setImage:[UIImage imageNamed:@"voice"] forState:UIControlStateNormal];
        [_voiceButton addTarget:self action:@selector(voiceButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }

    return _voiceButton;
}

- (UIButton*)albumButton{

    if(_albumButton==nil) {
        _albumButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_albumButton setTitle:@"相册 " forState:UIControlStateNormal];
        [_albumButton layoutButtonWithEdgeInsetsStyle:GMButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        _albumButton.titleLabel.font= [UIFont systemFontOfSize:14];
        [_albumButton setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
        [_albumButton setImage:[UIImage imageNamed:@"photo"] forState:UIControlStateNormal];
        [_albumButton addTarget:self action:@selector(albumButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _albumButton;
}

- (UIButton*)cameraButton{

    if (_cameraButton == nil) {
        _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cameraButton layoutButtonWithEdgeInsetsStyle:GMButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        _cameraButton.titleLabel.font=[UIFont systemFontOfSize:14];
        [_cameraButton setTitle:@"拍照 " forState:UIControlStateNormal];
        [_cameraButton setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
        [_cameraButton setImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
        [_cameraButton addTarget:self action:@selector(cameraButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _cameraButton;

}

- (UIButton*)videoButton{

    if(_videoButton==nil) {

        _videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_videoButton layoutButtonWithEdgeInsetsStyle:GMButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        [_videoButton setTitle:@"录像 " forState:UIControlStateNormal];
        _videoButton.titleLabel.font=[UIFont systemFontOfSize:14];
        [_videoButton setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
        [_videoButton setImage:[UIImage imageNamed:@"video"] forState:UIControlStateNormal];
        [_videoButton addTarget:self action:@selector(videoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }

    return _videoButton;
}

#pragma 按钮的点击

//语音

- (void)voiceButtonClick:(UIButton*)button{
//
//    if (self.voiceButtonBlock) {
//
//        self.voiceButtonBlock(button);
//
//    }

}

//相册

- (void)albumButtonClick:(UIButton*)button{

//    if (self.albumButtonBlock) {
//
//        self.albumButtonBlock(button);
//
//    }

}

//拍照

- (void)cameraButtonClick:(UIButton*)button{

//    if (self.cameraButton) {
//
//        self.cameraButtonBlock(button);
//
//    }

}

//录像

- (void)videoButtonClick:(UIButton*)button{

//    if (self.videoButtonBlock) {
//
//        self.videoButtonBlock(button);
//    }
}

@end
上一篇下一篇

猜你喜欢

热点阅读