Simple UIStackView breaks constraints

Viewed 44

I have a UIStackView inside a UIViewRepresentable but I am unable to get it working without constraint errors.

This is the error:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x6000028edf40 h=--& v=--& benjamin_ios.ConversationChatViewWithSendMessage:0x7fef7323a610.height == 0   (active)>",
    "<NSLayoutConstraint:0x600002893c00 UIView:0x7fef7322a3d0.height == 100   (active)>",
    "<NSLayoutConstraint:0x600002891d10 V:|-(0)-[UIStackView:0x7fef7320c450]   (active, names: '|':benjamin_ios.ConversationChatViewWithSendMessage:0x7fef7323a610 )>",
    "<NSLayoutConstraint:0x600002891c70 UIStackView:0x7fef7320c450.bottom == benjamin_ios.ConversationChatViewWithSendMessage:0x7fef7323a610.bottom   (active)>",
    "<NSLayoutConstraint:0x6000028e6170 'UISV-canvas-connection' UIStackView:0x7fef7320c450.top == UIView:0x7fef73229830.top   (active)>",
    "<NSLayoutConstraint:0x6000028e4050 'UISV-canvas-connection' V:[UIView:0x7fef7322a3d0]-(0)-|   (active, names: '|':UIStackView:0x7fef7320c450 )>",
    "<NSLayoutConstraint:0x6000028ecc30 'UISV-spacing' V:[UIView:0x7fef73229830]-(0)-[UIView:0x7fef7322a3d0]   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002893c00 UIView:0x7fef7322a3d0.height == 100   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

This is the code:

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)

        backgroundColor = .red

        let otherView = UIView()

        otherView.backgroundColor = .blue
        otherView.translatesAutoresizingMaskIntoConstraints = false

        let otherView2 = UIView()

        otherView2.backgroundColor = .brown
        otherView2.translatesAutoresizingMaskIntoConstraints = false

        let stack = UIStackView(arrangedSubviews: [otherView, otherView2])

        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.axis = .vertical
        stack.distribution = .fill
        stack.alignment = .fill

        addSubview(stack)

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

        otherView2.heightAnchor.constraint(equalToConstant: 100).isActive = true
    }
}

This is what I want to achieve (The display actually is what I want but I want to get rid of the constraint errors):

enter image description here

1 Answers

The solution was to give all the constraints I created the defaultHigh priority. This has to do something with SwiftUI porting to UIKit since the code just works fine in plain UIKit.

Related