【发布时间】:2020-08-23 05:36:25
【问题描述】:
我正在尝试通过 UIHostingController 将 SwiftUI 视图作为附件视图添加到 UITextView。以下是我的UIInputView 配置
struct AccessoryView: View { /* SwiftUI View */ }
class AccessoryInputView: UIInputView {
private let controller: UIHostingController<AccessoryView>
init(_ attachmentItem: Binding<AttachmentItemType>) {
let parentView = AccessoryView(selectedAttachmentItem: attachmentItem)
self.controller = UIHostingController<AccessoryView>(rootView: parentView)
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 44), inputViewStyle: .default)
self.controller.view.translatesAutoresizingMaskIntoConstraints = false
self.controller.view.backgroundColor = UIColor.clear
self.addSubview(self.controller.view)
self.layer.borderWidth = 2
self.layer.borderColor = UIColor.blue.cgColor
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
NSLayoutConstraint.activate([
self.widthAnchor.constraint(equalTo: self.controller.view.widthAnchor),
self.heightAnchor.constraint(equalTo: self.controller.view.heightAnchor),
self.centerXAnchor.constraint(equalTo: self.controller.view.centerXAnchor),
self.centerYAnchor.constraint(equalTo: self.controller.view.centerYAnchor)
])
}
}
我用蓝色绘制了UIInputView 的边框,并在AccessoryView 周围设置了一个红色边框。正如您在屏幕截图中看到的,SwiftUI 视图偏移了几个点。 UITextView 的边框为绿色。
发生这种情况的任何原因?
【问题讨论】:
标签: ios swift uiview swiftui inputaccessoryview