立即注册 登录
即时通讯网 返回首页

Ryun的个人空间 http://www.52im.net/?28523 [收藏] [复制] [RSS]

日志

SwiftUI 集成 IMKit

已有 141 次阅读2024-01-11 11:30 |个人分类:IM知识

Swift UI 集成 IMKit
在 SwiftUI 中,集成 IMKit 中 RCConversationListViewController 和 RCConversationViewController 两页面可能存在以下问题:
1、直接在 View 中使用,两个页面会错位;
2、用 NavigationView 集成会话列表和会话页面,标题失效;

分析
这两个页面强依赖导航 UINavigationController,并且内部用的 frame 结算的布局。直接用放入 View 中会导致安全区域失效,导致 frame 计算错误。

struct ContentView: View {
var body: some View {
ChatListView()
}
}
在 SwiftUI 中,UIKit 的 UINavigationController 和 NavigationView 的标题兼容性不太好,在二级页面设置 navigationBar 的 title 不生效。

解决方案
开发者需要用 UIViewControllerRepresentable 将 UIKit 的页面转换一下:

struct ChatListView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
let displayConversationTypeArray = [
RCConversationType.ConversationType_PRIVATE.rawValue,
RCConversationType.ConversationType_GROUP.rawValue,
]
let collectionConversationType = [
RCConversationType.ConversationType_SYSTEM.rawValue
]
guard let conversationList = RCDChatsViewController(
displayConversationTypes: displayConversationTypeArray,
collectionConversationType: collectionConversationType
) else {
return UIViewController()
}
return UINavigationController(rootViewController: conversationList);
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
然后在合适的地方使用,比如 ContentView 中,注意,需要使用 ignoresSafeArea 忽略安全区域。

struct ContentView: View {
var body: some View {
ChatListView()
.ignoresSafeArea()
}
}
0 推荐

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

返回顶部