
IM即时通讯
安全可靠、全球互通

实时音视频
流畅稳定、省钱省力
回答:
实现流程:
1. 在聊天页面添加一个 UIView 属性,作为自定义 View 的对象。
@property (nonatomic, strong) UIView *needAddView;
2. 在 viewWillAppear 生命周期函数中添加 needAddView,并设置 UI 布局,保证进入页面时,needAddView 可以正确显示,代码如下:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //初始化 needAddView,添加到 self.view 上,坐标 y = 输入框的 y 坐标 - needAddView 高度 CGFloat needAddView_height = 50.f; CGFloat y = self.chatSessionInputBarControl.frame.origin.y - needAddView_height; self.needAddView = [[UIView alloc] initWithFrame:CGRectMake(0, y, self.conversationMessageCollectionView.frame.size.width, needAddView_height)]; self.needAddView.backgroundColor = [UIColor blueColor]; [self.view addSubview:self.needAddView]; //设置消息内容 collectionView 的高度,要减去 needAddView 的高度,避免被遮挡。 CGRect frame = self.conversationMessageCollectionView.frame; frame.size.height -= needAddView_height; self.conversationMessageCollectionView.frame = frame; [self.conversationMessageCollectionView setContentOffset:CGPointMake(0,frame.size.height)]; }
3. 当输入框位置变化时,对于 UI 布局当改变,需要重写相关方法,代码如下:
-(void)chatInputBar:(RCChatSessionInputBarControl *)chatInputBar shouldChangeFrame:(CGRect)frame { //切记要调用父类方法,保证 UI 布局显示正确 [super chatInputBar:chatInputBar shouldChangeFrame:frame]; //needAddView 的坐标 y = 输入框的 y 坐标 - needAddView 高度。回调方法中的 frame 是输入框改变后的值。 CGRect viewFrame = self.needAddView.frame; viewFrame.origin.y = frame.origin.y - viewFrame.size.height; self.needAddView.frame = viewFrame; //设置消息内容 collectionView 的高度,要减去 needAddView 的高度,避免被遮挡。 CGRect collectionViewFrame = self.conversationMessageCollectionView.frame; collectionViewFrame.size.height -= self.needAddView.frame.size.height; self.conversationMessageCollectionView.frame = collectionViewFrame; //重新设置消息内容 collectionView 的 ContentOffset,正常显示消息内容。 if (self.conversationMessageCollectionView.contentSize.height > collectionViewFrame.size.height) { [self.conversationMessageCollectionView setContentOffset:CGPointMake(0, self.conversationMessageCollectionView.contentSize.height - collectionViewFrame.size.height) animated:NO]; } }