SwiftUI视图作为UITextView inputAccessoryView具有错误的框架

问题描述

我正在尝试通过UITextView将SwiftUI视图作为附件视图添加到UIHostingController。以下是我的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的边框为绿色。

为什么会这样?

here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)