自适应双栏会话容器
IMKit SDK 从 5.48.0 版本开始提供 RCConversationContainerViewController,用于 iPhone Duo 等需要自适应单双栏布局的场景。
RCConversationContainerViewController 统一管理会话列表页面和会话页面及其导航。空间和系统布局条件允许时,会话列表与会话页面并排显示;空间不足时,自动切换为单栏,在列表和会话页面之 间导航。
双栏布局下,未打开会话时详情栏显示占位页。单双栏切换时,容器保留当前会话页面及其后续页面的导航状态。
初始化
使用 initWithConversationListViewController: 传入会话列表控制器,创建会话容器。您可以传入 RCConversationListViewController 或其子类。
接口原型
- (instancetype)initWithConversationListViewController:(RCConversationListViewController *)listViewController;
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
listViewController | RCConversationListViewController | 会话列表控制器,必须尚未属于其他父控制器。 |
示例代码
以下代码在 UITabBarController 子类中执行,将会话容器作为一个 Tab 页面。其他 Tab 可按应用现有方式配置。
NSArray *conversationTypes = @[@(ConversationType_PRIVATE),
@(ConversationType_GROUP),
@(ConversationType_SYSTEM)];
RCConversationListViewController *listViewController =
[[RCConversationListViewController alloc]
initWithDisplayConversationTypes:conversationTypes
collectionConversationType:nil];
RCConversationContainerViewController *container =
[[RCConversationContainerViewController alloc]
initWithConversationListViewController:listViewController];
container.tabBarItem.title = @"消息";
self.viewControllers = @[container];
- 双栏布局需要 iOS 14 及以上版本,更低版本始终使用单栏布局。
- 容器内部已管理导航控制器,应直接放入 Tab 或其他父容器,不要在外部再次包装
UINavigationController。 - 容器的初始化、配置和页面操作均须在主线程执行。
打开会话
使用默认列表点击行为
如果没有重写会话列表的 onSelectedTableRow:conversationModel:atIndexPath: 方法,接入容器后,点击会话会由 SDK 自动创建并呈现会话页面。
如需通过已有的 RCConversationModel 主动打开会话,可调用 showConversationModel:animated:。下文中的 container 为初始化时创建的会话容器,model 为目标会话的数据模型。
接口原型
- (void)showConversationModel:(RCConversationModel *)model animated:(BOOL)animated;
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
model | RCConversationModel | 目标会话的数据模型。 |
animated | BOOL | 是否在支持动画的导航操作中使用过渡动画。 |
示例代码
[container showConversationModel:model animated:YES];
使用已配置的会话页面
如果已重写列表点击事件,并自行创建、配置会话页面,可保留原有业务逻辑,将最终的 pushViewController:animated: 调用替换为列表的 showConversationViewController:animated:。
以下示例在 RCConversationListViewController 子类中重写点击事件。会话页面可替换为应用自定义的 RCConversationViewController 子类。
示例代码
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
conversationModel:(RCConversationModel *)model
atIndexPath:(NSIndexPath *)indexPath {
RCConversationViewController *conversation =
[[RCConversationViewController alloc]
initWithConversationType:model.conversationType targetId:model.targetId];
conversation.channelId = model.channelId;
conversation.title = model.conversationTitle;
conversation.unReadMessage = model.unreadMessageCount;
conversation.enableNewComingMessageIcon = YES;
conversation.enableUnreadMessageIcon = YES;
[self showConversationViewController:conversation animated:YES];
}
showConversationViewController:animated: 在容器中会根据当前布局呈现页面;列表独立使用时,仍通过列表所属的导航控制器打开页面。自行处理呈现时,不要再调用默认点击实现,以免重复打开会话。已实现聚合会话等点击分支的应用,应保留原有分支处理,仅替换打开具体会话页面的动作。
搜索结果、通知等其他入口可以直接调用容器的 presentConversationViewController:animated:,传入已完成业务配置、尚未属于其他父控制器的会话页面。
RCConversationViewController *conversation =
[[RCConversationViewController alloc]
initWithConversationType:ConversationType_PRIVATE targetId:@"targetId"];
conversation.title = @"会话标题";
[container presentConversationViewController:conversation animated:YES];
- 调用
showConversationModel:animated:时,会话类型、会话 ID 和频道 ID 均与当前会话一致,则复用当前页面,并返回该会话的根页面。 - 调用
presentConversationViewController:animated:时,传入当前会话页面实例会返回该会话的根页面;传入不同实例会替换当前会话页面及其后续页面,即使两个实例的会话标识相同。列表的showConversationViewController:animated:在容器中使用同样的呈现规则。
定制化
自定义会话页面
您可以通过 conversationViewControllerFactory 配置会话页面的创建方式。默认列表点击和 showConversationModel:animated: 需要创建新页面时,会调用此回调。
回调应返回已完成业务配置、尚未属于其他父控制器的 RCConversationViewController 或其子类。返回 nil 表示取消本次打开操作。未设置回调时,由 SDK 创建和配置默认会话页面。
示例代码
container.conversationViewControllerFactory =
^RCConversationViewController *(RCConversationModel *model) {
RCConversationViewController *conversation =
[[RCConversationViewController alloc]
initWithConversationType:model.conversationType targetId:model.targetId];
conversation.channelId = model.channelId;
conversation.title = model.conversationTitle;
conversation.unReadMessage = model.unreadMessageCount;
conversation.enableNewComingMessageIcon = YES;
conversation.enableUnreadMessageIcon = YES;
return conversation;
};
复用当前会话,或通过 showConversationViewController:animated:、presentConversationViewController:animated: 传入已配置页面时,不会调用 conversationViewControllerFactory。