美文网首页
自定义网易IM输入框

自定义网易IM输入框

作者: 東玖零 | 来源:发表于2021-05-02 16:36 被阅读0次

感慨:也许是iOS没落了,也许是大神不需要记笔记,网上搜索的较少。
关键字:NIMKit、NIMInputView、NIMInputToolBar
需求是在输入框上面加一段显示几个按钮:


WX20210502-162208@2x.png

由于IM的代码量比较大,就来些主要代码的片段:

@interface NIMInputToolBar : UIView
/////////////声明一个view
@property (nonatomic,strong) UIView      *topView;
/////////////下面有很多原生代码
@property (nonatomic,strong) UIButton    *voiceButton;

@end

@implementation NIMInputToolBar

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        /////////////初始化一个view
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.nim_width, 40)];
        [self addSubview:_topView];
        /////////////下面有很多原生代码
        _voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_voiceButton setImage:[UIImage nim_imageInKit:@"icon_toolview_voice_normal"] forState:UIControlStateNormal];
        [_voiceButton setImage:[UIImage nim_imageInKit:@"icon_toolview_voice_pressed"] forState:UIControlStateHighlighted];
        [_voiceButton sizeToFit];
}

// 计算高度的时候加上topview的高度
- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat viewHeight = 0.0f;
    if (self.status == NIMInputStatusAudio) {
        viewHeight = 54.5  + _topView.nim_height;
    } else {
        //算出 TextView 的宽度
        [self adjustTextViewWidth:size.width];
        // TextView 自适应高度
        [self.inputTextView layoutIfNeeded];
        viewHeight = (int)self.inputTextView.frame.size.height;
        //得到 ToolBar 自身高度
        viewHeight = viewHeight + 2 * self.spacing + 2 * self.textViewPadding + _topView.nim_height;
    }
    return CGSizeMake(size.width,viewHeight);
}

// 刷新各控件时把topview的位置留出来
- (void)layoutSubviews {
    [super layoutSubviews];
    if ([self.types containsObject:@(NIMInputBarItemTypeTextAndRecord)]) {
        self.inputTextBkgImage.nim_width  = self.inputTextView.nim_width  + 2 * self.textViewPadding;
        self.inputTextBkgImage.nim_height = self.inputTextView.nim_height + 2 * self.textViewPadding;
    }
    CGFloat left = 0;
    for (NSNumber *type in self.types) {
        UIView *view = [self subViewForType:type.integerValue];
        if (!view.superview)
        {
            [self addSubview:view];
        }
        view.nim_left = left + self.spacing;
//        view.nim_centerY = self.nim_height * .5f;
        view.nim_centerY = self.topView.nim_height + (self.nim_height - self.topView.nim_height) * .5f;
        left = view.nim_right;
    }

@end

源码的修改就是上面,注意topView相关代码
当我们自定义聊天页面SessionViewController继承NIMSessionViewController这个类。
在SessionViewController的相关的代码如下:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self toolBarTopView];
    });
}

- (void)toolBarTopView {
//    self.sessionInputView.toolBar.topView
    CGFloat height = self.sessionInputView.toolBar.topView.height;
    self.sessionInputView.toolBar.topView.backgroundColor = kGrayColorA;
    NSArray *titles = @[@"天天打卡",@"能量状态", @"智能方案", @"在线课程"];
    CGFloat width = k_screen_width/titles.count;
    for (int i = 0; i < titles.count; i ++) {
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(width*i, 0, width, height);
        bt.titleLabel.font = kFont14;
        bt.tag = i;
        [bt setTitle:titles[i] forState:UIControlStateNormal];
        [bt setTitleColor:kBlackColorA forState:UIControlStateNormal];
        [bt addTarget:self action:@selector(topViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.sessionInputView.toolBar.topView addSubview:bt];
    }
}

运行最后的效果:


image.png

相关文章

网友评论

      本文标题:自定义网易IM输入框

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