swiftui - SwiftUI 上下文菜单是否使用 LayoutConstraints?

标签 swiftui

我假设 SwiftUI 不再使用 NSLayoutContstraints。但是我在控制台中收到约束错误。任何人都知道如何调试?

当我打开上下文菜单时,如果使用 ListView ,则会打印出以下内容:

2019-12-09 10:52:52.029091-0700 ContextMenuTest[26384:13138419] [LayoutConstraints] 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) ( "", "= 44 (active, names: groupView.actionsSequence...:0x7fd98781de00 )>", "", "", "", "" )

Will attempt to recover by breaking constraint = 44 (active, names: groupView.actionsSequence...:0x7fd98781de00 )>

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


struct ContentView: View {
    var body: some View {
        List {
            Text("one")
            Text("two")
                .contextMenu(menuItems: {
                    Text("test")
                })
        }
    }
}

最佳答案

I was under the assumption that SwiftUI no longer uses NSLayoutContstraints.



这只是 不是 真的。 SwiftUI仍在使用 UIKit组件,如果您检查 View 层次结构,并且大多数组件没有公开的约束。但是一些“桥接”的观点有NSLayoutConstraints为他们的基地UIView引擎盖下的类。

警报、弹出窗口和上下文菜单等某些组件也会发现您的问题,因为这些组件很复杂,因此尚未完全移植。

UIViewAlertForUnsatisfiableConstraints 使用符号断点:
po UIApplication.shared.windows.first?.constraints

( UIWindowUIView 的子类) https://developer.apple.com/documentation/uikit/uiwindow .

根托管 Controller 及其 subview 没有限制,因为它们已完全移植到 SwiftUI 新环境变量语法。
po UIApplication.shared.windows.first?.rootViewController?.view.constraints

SwiftUI 的很多运行时库还有 NSLayoutConstraints .见:context menu , alert view等等。

请注意,您需要切换到主堆栈帧才能访问 UIApplication.shared ( AppDelegate )。见下文:

Debug Navigator snippet

如何检查 View 层次结构?

使用 dump , 允许您查看 SwiftUI 信息(不仅仅是 po):
po dump(UIApplication.shared.windows.first?.rootViewController)

为 Controller 列出了 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 成熟并退出测试版,这些桥接可能会被删除。

关于swiftui - SwiftUI 上下文菜单是否使用 LayoutConstraints?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59254236/

相关文章:

swiftui - TextEditor 的动态高度

swiftui - 处理 iOS 17 中的数据流并在 TextField 中使用观察到的变量

swift - 在 SwiftUI 中同时打开多个预览

ios - 如何更改 SwiftUI 中导航栏的背景颜色?

SwiftUI:用户使用 DocumentPicker 选择文件后显示数组中的文件 url

swift - 在 SwiftUI 中使用可选项进行条件渲染

ios - 如何使 SwiftUI 循环中的元素全屏显示?与 Apple 一样,当今的应用程序

SwiftUI - NavigationLink 返回上一个 View

swift - 为什么列表中带有导航链接的项目变成蓝色?

ios - 在 SwiftUI 中拖动分隔符