SwiftUI上下文菜单使用布局约束吗?

8
我原以为SwiftUI不再使用NSLayoutContstraints,但是我在控制台中遇到了约束错误。有人知道如何调试吗?
当我打开上下文菜单时,以下内容会在列表视图中打印出来:
(LayoutConstraints)无法同时满足约束条件。 可能以下列表中的至少一个约束条件不是您想要的。 请尝试: (1)查看每个约束条件并尝试找出哪个不符合预期; (2)查找添加了不需要的约束条件的代码并予以修正。 (注意:如果您看到了自己无法理解的 NSAutoresizingMaskLayoutConstraints,请参考 UIView 属性translatesAutoresizingMaskIntoConstraints的文档) ( "", "= 44 (active, names: groupView.actionsSequence...:0x7fd98781de00 )>", "", "", "", "" )
将尝试通过打断约束条件= 44(active,names:groupView.actionsSequence ...:0x7fd98781de00)>来恢复。
可以在UIViewAlertForUnsatisfiableConstraints处设置符号性断点,以在调试器中捕获此错误。 列在UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
struct ContentView: View {
    var body: some View {
        List {
            Text("one")
            Text("two")
                .contextMenu(menuItems: {
                    Text("test")
                })
        }
    }
}

你找到解决方案了吗? - mica
1个回答

2
我原本以为SwiftUI不再使用NSLayoutConstraints。
但实际上并非如此。如果你检查视图层次结构,你会发现SwiftUI仍然使用UIKit组件,大多数组件没有暴露出约束。但是一些“桥接”视图在其基本的UIView类下面有NSLayoutConstraints。
您遇到的问题也可以在某些组件中看到,例如警报、弹出窗口和上下文菜单,因为这些组件比较复杂,所以尚未完全移植。
您可以使用符号断点UIViewAlertForUnsatisfiableConstraints来进行调试。
po UIApplication.shared.windows.first?.constraints

`UIWindow`是`UIView`的子类。根托管控制器及其子视图没有约束,因为它们已经完全移植到SwiftUI的新环境变量语法中。https://developer.apple.com/documentation/uikit/uiwindow
po UIApplication.shared.windows.first?.rootViewController?.view.constraints

许多SwiftUI的运行时库仍然使用NSLayoutConstraints。例如:上下文菜单警告视图等。
请注意,您需要切换到主堆栈帧才能访问UIApplication.sharedAppDelegate)。请参见下面: 调试导航器片段 如何检查视图层次结构?
使用dump命令,可以查看SwiftUI信息(比po更详细):
po dump(UIApplication.shared.windows.first?.rootViewController)

控制器中列出了UIKit桥接类,例如:
contextMenuBridge: Optional(<_TtGC7SwiftUI17ContextMenuBridgeV33Demo11ContentView_: 0x600002c8c720>)
       some: <_TtGC7SwiftUI17ContextMenuBridgeV33Demo11ContentView_: 0x600002c8c720> #81
        - super: NSObject
         host: Optional(<_TtGC7SwiftUI14_UIHostingViewV33Demo11ContentView_: 0x7fccd7403690; frame = (0 0; 414 896); autoresize = W+H; gestureRecognizers = <NSArray: 0x6000006f0d20>; layer = <CALayer: 0x6000008b5180>>)
           some: <_TtGC7SwiftUI14_UIHostingViewV33Demo11ContentView_: 0x7fccd7403690; frame = (0 0; 414 896); autoresize = W+H; gestureRecognizers = <NSArray: 0x6000006f0d20>; layer = <CALayer: 0x6000008b5180>> #0
         presentedMenuID: SwiftUI.ViewIdentity
          - seed: 0
        - interaction: nil
        - cachedPreferences: 0 elements
         seed: empty
          - value: 0
        - currentPreference: nil
        - cachedPlatformActions: 0 elements
        - cachedPreview: nil
    - accessibilityEnabled: false
    - cachedAccessibilityNodes: 0 elements
    - accessibilityNeedsUpdate: true
    - scrollTest: nil
    - delegate: nil
    - parentAccessibilityElement: nil

类似的窗口约束也可以在警报、表格和其他“桥接”类中找到。
使用以下方法修复错误:
UIApplication.shared.windows[0].translatesAutoresizingMaskIntoConstraints = false

请注意,这些桥可能会在SwiftUI成熟并退出beta版后被移除。

修复在我的情况下没有帮助...尝试在不同的位置(如SceneDelegate或直接在SwiftUI文件中) - smat88dd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接