Ios 无法使UIViewRepresentable的CustomView在SwiftUI中正常工作

Ios 无法使UIViewRepresentable的CustomView在SwiftUI中正常工作,ios,swiftui,Ios,Swiftui,我有这个SwiftUI组件 var body: some View { let image = TypographyImage.image(for: typography, kind: kind) let width = image.size.width + 4 let height = TypographyImage.height(for: typography) * (kind == .oneLine ? 1.0 : 2.0) ScrollView(.hor

我有这个SwiftUI组件

var body: some View {
    let image = TypographyImage.image(for: typography, kind: kind)
    let width = image.size.width + 4
    let height = TypographyImage.height(for: typography) * (kind == .oneLine ? 1.0 : 2.0)

    ScrollView(.horizontal) {
        HStack {
            Image(uiImage: image)
                    .frame(width: width, height: height, alignment: Alignment(horizontal: .center, vertical: .top))
                    .offset(y: TypographyImage.offset(for: typography))
                    .background(Color.red)
            if isSwiftUI {
                Text(text)
                        .typography(typography, theme: AmityTheme.shared)
                        .background(Color.red)
            } else {
                MyUILabel(text: text, typography: typography, theme: AmityTheme.shared).background(Color.red)
            }
        }
    }
}
哪个显示这个UI

请注意,在
MyUILabel
中,我应用了背景红色。 当在
MyUILabel
中返回
UILabel
作为第一个子视图时,
MyUILabel
内部
HStack
的背景色和对齐方式正确显示

如下所示

struct MyUILabel: UIViewRepresentable {
    var text: String
    var typography: AmityTheme.Typography
    var theme: AmityTheme

    func makeUIView(context: Context) -> UILabel {
        let view = UILabel(frame: .zero)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.setContentHuggingPriority(.defaultHigh, for: .vertical)
        view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        updateUIView(view, context: context)
        return view
    }

    func updateUIView(_ view: UILabel, context: Context) {
        view.attributedText = NSAttributedString(string: text, attributes: theme.textAttributes(for: typography, palette: .black))
    }
}
但是,如果我将其更改为返回自定义视图,如

struct MyUILabel: UIViewRepresentable {
    var text: String
    var typography: AmityTheme.Typography
    var theme: AmityTheme

    func makeUIView(context: Context) -> PaddedUILabel {
        let view = PaddedUILabel(frame: .zero)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.setContentHuggingPriority(.defaultHigh, for: .vertical)
        view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        updateUIView(view, context: context)
        return view
    }

    func updateUIView(_ view: PaddedUILabel, context: Context) {
        view.update(text: text, typography: typography, theme: theme)
    }
}

class PaddedUILabel: UIView {
    private var label: UILabel!
    private var topConstraint: NSLayoutConstraint!
    private var bottomConstraint: NSLayoutConstraint!

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    private func commonInit() {
        let label = UILabel(frame: .zero)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.numberOfLines = 0
        label.setContentHuggingPriority(.defaultHigh, for: .vertical)
        label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        self.label = label

        addSubview(label)

        topConstraint = label.topAnchor.constraint(equalTo: topAnchor)
        bottomConstraint = label.bottomAnchor.constraint(equalTo: bottomAnchor)
        let constraints: [NSLayoutConstraint] = [
            label.leadingAnchor.constraint(equalTo: leadingAnchor),
            label.trailingAnchor.constraint(equalTo: trailingAnchor),
            topConstraint,
            bottomConstraint
        ]
        NSLayoutConstraint.activate(constraints)
    }

    func update(text: String, typography: AmityTheme.Typography, theme: AmityTheme) {
        let attributes = theme.textAttributes(for: typography, palette: .black)
        label.attributedText = NSAttributedString(string: text, attributes: attributes)
    }
}
结果是

背景色消失,且
HStack
中心垂直对齐不正确。我似乎不知道为什么会这样

有人能给我建议如何解决这个问题吗


谢谢

看起来像是布局
CustomView
中的任何约束,并使其
UIViewRepresentable
无法根据

在SwiftUI中,家长问孩子:您想要多大尺寸?子对象告诉父对象,父对象设置大小。完成。 但是,自动布局需要多次通过才能确定完整的布局。它根本没有机会

我对UIViewRepresentable的经验法则是:让它超级简单,只包装一个视图。不止一个视图询问布局问题


还指出,SwiftUI布局的工作方式有一个限制,它与普通布局约束不兼容,
intrinsincontentsize

为什么要使用自定义视图paddeDiLabel(UILabel)?因为这是我们应用程序的一种核心框架,我必须确保我们的主题和NSAttributedString在SwiftUI和UIKit中都能正常工作。
PaddedUILabel
将在框架端实现,以使显示的文本与设计器预期的文本完全相同(
NSAttributedString
的键属性不够)。这就是为什么要制作
PaddedUILabel