程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConstraints。为什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConsTraints。为什么??

开发过程中遇到简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConsTraints。为什么?的问题如何解决?下面主要结合日常开发的经验,给出你关于简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConsTraints。为什么?的解决方法建议,希望对你解决简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConsTraints。为什么?有所启发或帮助;

环境:Xcode 版本 12.4 (12D4E)

场景:发现了一个明显违反约束的区域。
我将范围缩小到 .navigationtitle。

import SwiftUI

struct ContentVIEw: VIEw {
    var body: some VIEw {
        NavigationVIEw {
            vstack {
                Text("Hello World")
            }.navigationtitle("Turkey")
        }
    }
}

就是这样。
如果我删除 .navigationtitle(),它就会清除。

以下是控制台中列出的约束违规:


2021-02-09 19:14:38.578190-0800 Navbarcheck[6300:437683] [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. 
(
    "<NSLayoutConsTraint:0x600003149ef0 'BIB_Trailing_CB_Leading' H:[_UIModernbarbutton:0x12c01a770]-(6)-[_UIModernbarbutton:0x12c0191e0'Turkey']   (activE)>","<NSLayoutConsTraint:0x600003149f40 'CB_Trailing_Trailing' _UIModernbarbutton:0x12c0191e0'Turkey'.Trailing <= _UIbuttonbarbutton:0x12c018560.Trailing   (activE)>","<NSLayoutConsTraint:0x60000314ACB0 'UINav_static_button_horiz_position' _UIModernbarbutton:0x12c01a770.leading == UILayoutGuIDe:0x600002b65260'UIVIEwLayoutmarginsGuIDe'.leading   (activE)>","<NSLayoutConsTraint:0x60000314ad00 'UINavItemContentGuIDe-leading' H:[_UIbuttonbarbutton:0x12c018560]-(0)-[UILayoutGuIDe:0x600002b65180'UINavigationbarItemContentLayoutGuIDe']   (activE)>","<NSLayoutConsTraint:0x60000314c780 'UINavItemContentGuIDe-Trailing' UILayoutGuIDe:0x600002b65180'UINavigationbarItemContentLayoutGuIDe'.Trailing == _UINavigationbarContentVIEw:0x12ae0f100.Trailing   (activE)>","<NSLayoutConsTraint:0x60000314b3e0 'UIVIEw-Encapsulated-Layout-WIDth' _UINavigationbarContentVIEw:0x12ae0f100.wIDth == 0   (activE)>","<NSLayoutConsTraint:0x60000314cb40 'UIVIEw-leftmargin-guIDe-consTraint' H:|-(0)-[UILayoutGuIDe:0x600002b65260'UIVIEwLayoutmarginsGuIDe'](LTR)   (active,names: '|':_UINavigationbarContentVIEw:0x12ae0f100 )>"
)

Will attempt to recover by breaking consTraint 
<NSLayoutConsTraint:0x600003149ef0 'BIB_Trailing_CB_Leading' H:[_UIModernbarbutton:0x12c01a770]-(6)-[_UIModernbarbutton:0x12c0191e0'Turkey']   (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.

问题:这是一个错误吗?或者如果没有,发生了什么和补救措施?

解决方法

@H_673_32@

我研究了这个问题很长时间,这就是我得出的结论。我认为它会很有用:

导航视图的一个有趣之处在于它如何在大型设备(通常是 iPhone 和大型 iPad)上执行分屏功能。

var body: some View {
        NavigationView {
            Text("PriMary")
        }
    }

如果将 iPhone 11 ProMax 旋转到横向(Cmd + ->),您会看到文本视图消失了。

SwiftUI 会自动虑横向导航视图并显示 DetailView 而不是 main(“主要”)。

您可以通过在 NavigationView 中提供两个视图来解决 SwiftUI 期望的问题,例如:

var body: some View {
        NavigationView {
            Text("PriMary")
            Text("Secondary")
        }
    }

由于本人名气不大,所以只能发个带链接的照片(如果我的帖子有用,请给个好评): enter image description here

这就是为什么我们添加. navigation Bar title(Text ("Today's Flavors"),displaymode:. inlinE)

时会抛出错误“无法同时满足约束”的原因

因此,解决方法如下:

  1. 向导航 Vi 添加第二个视图

在这里,当你旋转屏幕时,屏幕 #2 - Text("Secondary") 将被显示。

    var body: some View {
            NavigationView {
                Text("PriMary")
                Text("Secondary")
            .navigationBartitle(Text("Today's Flavors"),displaymode: .inlinE)
            }
    }
  1. 修饰符 .navigationViewStyle(StackNavigationViewStyle())

但是,如果要明确指定旋转时始终显示第一个屏幕(Text("PriMary")),则需要添加 .navigationViewStyle(StackNavigationViewStylE) modifier()) ,它可以让您始终切换到 Text (“主要”),无论屏幕如何。

var body: some View {
        NavigationView {
            Text("PriMary")
            Text("Secondary")
        .navigationBartitle(Text("Today's Flavors"),displaymode: .inlinE)
        }
        .navigationViewStyle(StackNavigationViewStyle())

此外,您还可以阅读有关 NavigationView here

的更多信息 ,

NavigationBartitle 和 Navigationtitle 已从 iOS 14.3 开始弃用。尝试在导航视图上添加 .navigationViewStyle(StackNavigationViewStyle())

此解决方案的所有功劳都归功于 https://stackoverflow.com/a/66024249/15184473

大佬总结

以上是大佬教程为你收集整理的简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConstraints。为什么?全部内容,希望文章能够帮你解决简单的 SwiftUI 代码产生: UIViewAlertForUnsatisfiableConstraints。为什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:简单的