SwiftUI 集成 IMKit

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()
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值