简单的 UIStackView 打破了限制

发布于 2025-01-11 20:57:21 字数 3811 浏览 0 评论 0原文

我在 UIViewRepresentable 中有一个 UIStackView,但我无法让它在没有约束错误的情况下工作。

这是错误:

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.

这是代码:

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
    }
}

这是我想要实现的目标(显示实际上是我想要的,但我想摆脱约束错误):

在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2025-01-18 20:57:21

解决方案是为我创建的所有约束赋予defaultHigh优先级。这与 SwiftUI 移植到 UIKit 相关,因为代码在普通 UIKit 中运行良好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文