SwiftUI 上下文菜单是否使用 LayoutConstraints? [英] Do SwiftUI Context Menus use LayoutConstraints?

查看:21
本文介绍了SwiftUI 上下文菜单是否使用 LayoutConstraints?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was under the assumption that SwiftUI no longer uses NSLayoutContstraints. However I am getting constraint errors in console. Anyone have any idea how to debug?

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

The following prints out if with the list view when I open the context menu:

2019-12-09 10:52:52.029091-0700 ContextMenuTest[26384:13138419] [LayoutConstraints] 无法同时满足约束.可能以下列表中的至少一项约束是您不想要的.尝试这个:(1) 查看每个约束并尝试找出您不期望的;(2) 找到添加不需要的约束或约束的代码并修复它.(注意:如果您看到 NSAutoresizingMaskLayoutConstraints 不明白,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)("","= 44 (active, names: groupView.actionsSequence...:0x7fd98781de00)>","","","",")

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 )>", "", "", "", "" )

将尝试通过打破约束来恢复= 44(活动,名称:groupView.actionsSequence...:0x7fd98781de00)>

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

在 UIViewAlertForUnsatisfiableConstraints 处创建一个符号断点以在调试器中捕获它.中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助.

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

推荐答案

我假设 SwiftUI 不再使用NSLayoutContstraints.

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

这完全是真的.如果您检查视图层次结构,SwiftUI 仍然使用 UIKit 组件,并且大多数组件没有暴露的约束.但是一些桥接"视图在其底层具有 NSLayoutConstraints 用于它们的基本 UIView 类.

This is simply not true. SwiftUI still uses UIKit components if you inspect the view hierarchy, and most components do not have exposed constraints. But some "bridged" views have NSLayoutConstraints for their base UIView classes under the hood.

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

Your issue is also seen by some components like alerts, popovers and context menus, as these are complex and so have not been fully ported.

UIViewAlertForUnsatisfiableConstraints 使用符号断点:

po UIApplication.shared.windows.first?.constraints

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

(UIWindow is a subclass of UIView) https://developer.apple.com/documentation/uikit/uiwindow.

根托管控制器及其子视图没有任何限制,因为它们已完全移植到 SwiftUI 新的环境变量语法.

The root hosting controller and its subviews have no constraints as they have been fully ported to SwiftUI new Environment Variable syntax.

po UIApplication.shared.windows.first?.rootViewController?.view.constraints

SwiftUI 的许多运行时库仍然具有 NSLayoutConstraints.请参阅:上下文菜单, andalert以此类推.

Many runtime libraries of SwiftUI still have NSLayoutConstraints. See: context menu, alert view and so on.

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

Note you need to switch to the main stack frame in order to be able to access UIApplication.shared (AppDelegate). See below:

调试导航器片段

如何检查视图层次结构?

使用 dump,可以让你看到 SwiftUI 信息(不仅仅是 po):

Using dump, allows you to see the SwiftUI info (more than po):

po dump(UIApplication.shared.windows.first?.rootViewController)

为控制器列出了 UIKit 桥接类,例如

The UIKit bridging classes are listed for the controller, e.g

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

可以为警报、工作表和其他桥接"类找到类似的窗口约束.

Similar window constraints can be found for alerts, sheets and other "bridged" classes.

使用以下方法修复错误:

Fix the error using:

UIApplication.shared.windows[0].translatesAutoresizingMaskIntoConstraints = false

但是请注意,一旦 SwiftUI 成熟并退出测试版,这些桥接器可能会被删除.

Note however that these bridges may be removed once SwiftUI matures and comes out of beta.

这篇关于SwiftUI 上下文菜单是否使用 LayoutConstraints?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆