Home > Mobile >  UIViewRepresentable remains white
UIViewRepresentable remains white

Time:03-06

I have this very basic setup:

struct ConversationChatMessagesWrapper: UIViewRepresentable {
    private let view = ConversationChatViewWithSendMessage()

    func makeUIView(context _: Context) -> ConversationChatViewWithSendMessage {
        view
    }

    func updateUIView(
        _: ConversationChatViewWithSendMessage,
        context _: Context
    ) {}
}


class ConversationChatViewWithSendMessage: UIView {
    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("Not used")
    }

    init() {
        super.init(frame: .zero)

        translatesAutoresizingMaskIntoConstraints = false

        backgroundColor = .red

        let otherView = UIView()

        otherView.backgroundColor = .blue
        otherView.translatesAutoresizingMaskIntoConstraints = false

        addSubview(otherView)

        otherView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        otherView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        otherView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        otherView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    }
}

I haven't worked with UIKit in a while, but I thought it should see a blue screen, or at least a red screen, but my screen remains white. When I remove the call which adds the otherView to ConversationChatViewWithSendMessage, I see a blue screen, so something is wrong while adding otherView, but I can't see what. I even replicated this setup in Storyboard and that just worked fine.

Any suggestions?

This is a screenshot from Xcode, the warning states:

Position and size are ambigious

enter image description here

CodePudding user response:

Remove this line after the super.init.

translatesAutoresizingMaskIntoConstraints = false
  • Related